Search code examples
wpfbindingstaticresource

wpf binding to static resource


So I have aproblem with binding in WPF. I am trying to bind a DataGrid ComboBoxColumn with a static resource but with no luck. I know where the problem is but I'm not sure how to fix it.

in XAML i have this:

<local:MyClasificators x:Key="clList"></local:MyClasificators>

and DataGridComboBoxColumn

<DataTemplate>
    <ComboBox ItemsSource="{StaticResource clList}" DisplayMemberPath="Value"  ></ComboBox>
</DataTemplate>

code for the source I'm binding:

public class MyClasificators:List<KeyValuePair<object, object>>
{
    public void _MyClasificators(DataTable country)
    {
        foreach (DataRow row in country.Rows)
        {
            this.Add(new KeyValuePair<object, object>(row.ItemArray[0], row.ItemArray[1]));
        }
    }

And the code for passing the DataTable:

public void callMyClassificators(DataTable country)
{
    MyClasificators clasif = new MyClasificators();
    clasif._MyClasificators(country);
}

I know that most probably I just have to edit the Resource part, but I'm not sure how should I go about it?


Solution

  • <local:MyClasificators x:Key="clList"></local:MyClasificators>
    

    translates to something like:

    Resources.Add("clList", new MyClasificators());
    

    That's it, there's no data in your object.

    You could create the resource clList from code, for example in app.xaml.cs:

    var countryTable = ... // Get or create table here
    var clList = new MyClasificators();
    var clList.callMyClassificators(countryTable);
    Resources.Add("clList", clList);