Click here to Skip to main content
14,733,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
A bit larger question this time but i hope it's only alot of code and the solution is short, simple and easy and the problem is only becouse of my temporary impairment.

I need to create ItemsSource to my CustomControl. The problem is the control is using to filter records in grid view so in MVVM pattern i need some databindings with various DataContexts.
Here are main parts of code:

View with GridView bounded to certain source:

<base:ViewBase.Resources>
    <observable:DocTypeNames x:Key="DocTypesList" Value="{Binding TypeNamesList}"/>
</base:ViewBase.Resources>


Static resource bounded to List in ViewModel (working fine - can be bounded to any control)

<wfcGrid:WFGridViewDataColumn Width="200" IsSortable="True" DataMemberBinding="{Binding CategoryName}">
    <wfcGrid:WFGridViewDataColumn.Header>
        <TextBlock TextWrapping="Wrap" Text="Kategoria dokumentu"/>
    </wfcGrid:WFGridViewDataColumn.Header>
    <wfcGrid:WFGridViewDataColumn.FilteringControl>
        <filters:ListSelectionFilter NamesList="{Binding Value,Source={StaticResource DocTypesList}}"/>
    </wfcGrid:WFGridViewDataColumn.FilteringControl>
    <wfcGrid:WFGridViewDataColumn.CellTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Margin="5" TextWrapping="Wrap" Text="{Binding CategoryName}"/>
            </StackPanel>
        </DataTemplate>
    </wfcGrid:WFGridViewDataColumn.CellTemplate>
</wfcGrid:WFGridViewDataColumn>

This is a part of GridView template thats why i cannot bind TypeNamesList directly.

The property in ListSelectionFilter control
public List<string> NamesList
{
    get
    {
        return (List<string>)GetValue(NamesListProperty);
    }
    set
    {
        if (itemsList == null)
        {
            itemsList = new List<ListItem>();
        }
        foreach (string s in value)
        {
            if (s != null)
            {
                itemsList.Add(new ListItem()
                {
                    Name = s,
                    Value = false,
                });
            }
        }
        SetValue(NamesListProperty, value);
        listBox.ItemsSource = itemsList;
    }
}

public static readonly DependencyProperty NamesListProperty =
    DependencyProperty.Register(&quot;NamesList&quot;, typeof(List&lt;string&gt;), typeof(ListSelectionFilter), new PropertyMetadata(new List&lt;string&gt;()));



And ObservableObject used as static resource.
public class DocTypeNames : ObservableObject<IEnumerable<string>>
   {
   }


What i know so far is:
- The only thing working iside the template is basic type declared in
<base:ViewBase.Resources>
<sys:String x:Key="Name">Nazwa</sys:String>       
</base:ViewBase.Resources>

When im using something like:
<wfcGrid:WFGridViewDataColumn.FilteringControl>
                                                    <filters:FromDateToDateFilterControl Name="{StaticResource Name}"/>
                                                </wfcGrid:WFGridViewDataColumn.FilteringControl>

Problem is when i'm using "Binding" expression.

- Enumerable Types/Interfaces doesn't matter. Or maybe i didn't found right one.

- Static resource with ObervableObject works fine with other Contexts when im binding for example:
<ComboBox x:Name="docTypeCombo" HorizontalAlignment="Left" Width="150" SelectedValuePath="Tree"

                      DisplayMemberPath="Name" SelectedValue="{Binding Path=DocumentType,Mode=TwoWay}" ItemsSource="{Binding Value,Source={StaticResource DocTypesList}}"

                      />

Combo box is inside DataForm with other DataContext but not in template. When i'm putting ComboBox inside DataForm.EditTemplate - its also working.

In my opinion the thing i'm doing wrong is declaration of ItemsSorce in custom control. Any tutorial i found shows that way but with source bound directly from ViewModel.

I'll be greatfull for any hint how to solve this.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900