1

I'm creating several static resources that I want to add to an ObjectDataProvider, but I can't figure out the syntax.

 <Window.Resources>

    <SolidColorBrush x:Key="SolidFillBrush" Color="Black" Opacity="1.0" />
    <SolidColorBrush x:Key="HalfOpaqueBrush" Color="Black" Opacity="0.5" />
    <SolidColorBrush x:Key="QuarterOpaqueBrush" Color="Black" Opacity="0.25" />
    <SolidColorBrush x:Key="TransparentBrush" Color="Black" Opacity="0" />

    <ObjectDataProvider x:Key="AllFillStyles" ObjectType="{x:Type Brush}" MethodName="???">
         <!-- add the static resources here, but how?  -->
    </ObjectDataProvider>

 </Window.Resources>

Any suggestions?

Edit: I was trying to create a ComboBox containing the above Brushes, so the user could select which brush to use as the fill style for a grid (kind of like in Excel, where you can choose the fill style and color.) I needed to set the ItemsSource, and found where someone had used the ObjectDataProvider. I figured out that you could create an Array in the xaml and fill it with the brushes, then use that instead.

Ed Beaty
  • 415
  • 1
  • 6
  • 14

2 Answers2

0

A sample one on how to use ObjectDataProvider. MethodName is the name of the Method it will try to call when getting the items

in your xaml file

<ObjectDataProvider x:Key="objDs"
                             ObjectType="{x:Type data:CDataAccess}"
                             MethodName="GetEmployees">
</ObjectDataProvider>

And your class file

public class CDataAccess
{
    ObservableCollection<ImageEmployee> _EmpCollection;

    public ObservableCollection<ImageEmployee> EmpCollection
    {
        get { return _EmpCollection; }
        set { _EmpCollection = value; }
    }

    public CDataAccess()
    {
        _EmpCollection = new ObservableCollection<ImageEmployee>();
    }

    public ObservableCollection<ImageEmployee> GetEmployees()
    {
        SqlConnection conn =
        new SqlConnection("Data Source=.;Initial Catalog=Company;" +
                            "Integrated Security=SSPI");
        SqlCommand cmd = new SqlCommand();
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = "Select * from ImageEmployee";

        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            EmpCollection.Add(
                new ImageEmployee()
                {
                    EmpNo = Convert.ToInt32(reader["EmpNo"]),
                    EmpName = reader["EmpName"].ToString(),
                    Salary = Convert.ToInt32(reader["Salary"]),
                    DeptNo = Convert.ToInt32(reader["DeptNo"]),
                    EmpImage = (byte[])reader["EmpImage"]
                });
        }
        reader.Close();
        conn.Close();
        return EmpCollection;
    }
}

If your MethodName has a parameter, you can add this to your resource object provider

<ObjectDataProvider.MethodParameters> 
                 <s:String>Parameter1</s:String> 
</ObjectDataProvider.MethodParameters>

and make sure your parameter types are matching it.

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
0

I don’t know what is yout real goal with ObjectDataProvider.

If you want call some method on object AllFillStyle with different parameter which identify resource key (SolidColorBrush object).

For example:

  • AllFileStyle.GetBrush("SolidFillBrush")
  • AllFileStyle.GetBrush("HalfOpaqueBrush")
  • etc

it is not easy because will need pass paramater dynamically to ObjectDataProvider.

If you only need group all resouces type of SolidColorBrush and use in XAML.

You can populate all resources type of SolidColorBrush in code.

private ObservableCollection<SolidColorBrush> _windowBrushes;

public ObservableCollection<SolidColorBrush> WindowBrushes
{
    get
    {
        return _windowBrushes;
    }

    set
    {
        _windowBrushes = value;
        OnPropertyChanged("WindowBrushes");
    }
}


private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
    WindowBrushes = new ObservableCollection<SolidColorBrush>();

    foreach (var resource in this.Resources.Values)
    {
        SolidColorBrush brush = resource as SolidColorBrush;

        if (brush!=null)
        {
            WindowBrushes.Add(brush);
        }
    }
}

And then use in XAML.

<ListBox x:Name="ListBoxBrushes"
         Grid.Row="0"
         Margin="5"
         ItemsSource="{Binding Path=WindowBrushes, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Ellipse Width="30"
                         Height="30"
                         Stroke="Black"
                         StrokeThickness="1"
                         Fill="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<Rectangle Grid.Row="1"
           Margin="5"
           HorizontalAlignment="Stretch"
           VerticalAlignment="Stretch"
           Fill="{Binding ElementName=ListBoxBrushes, Path=SelectedItem}"/>

Could you specify your scenario with ObjectDataProvider?

Thank

Community
  • 1
  • 1
Jan
  • 366
  • 1
  • 8