开发者

WPF data binding - what am I missing?

I am trying to grasp the concepts of WPF data binding through a simple example, but it seems I haven't quite gotten the point of all of it.

The example is one of cascading dropdowns; the XAML is as follows:

<Window x:Class="CascadingDropDown.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="496" Width="949" Loaded="Window_Loaded">
    <Grid>
        <ComboBox Name="comboBox1" ItemsSource="{Binding}" DisplayMemberPath="Key" SelectionChanged="comboBox1_SelectionChanged" />
        <ComboBox Name="comboBox2" ItemsSource="{Binding}" DisplayMemberPath="Name" />
    </Grid>
</Window>

This is the code of the form:

public partial class MainWindow : Window
{
    private ObservableCollection<ItemA> m_lstItemAContext = new ObservableCollection<ItemA>();
    private ObservableCollection<ItemB> m_lstItemBContext = new ObservableCollection<ItemB>();
    private IEnumerable<ItemB> m_lstAllItemB = null;

    public MainWindow()
    {
        InitializeComponent();

        this.comboBox1.DataContext = m_lstItemAContext;
        this.comboBox2.DataContext = m_lstItemBContext;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var lstItemA = new List<ItemA>() { new ItemA("aaa"), new ItemA("bbb"), new ItemA("ccc") };
        var lstItemB = new List<ItemB>() { new ItemB("aaa", "a11"), new ItemB("aaa", "a22"), new ItemB("bbb", "b11"), new ItemB("bbb", "b22") };

        initPicklists(lstItemA, lstItemB);
    }

    private void initPicklists(IEnumerable<ItemA> lstItemA, IEnumerable<ItemB> lstItemB)
    {
        this.m_lstAllItemB = lstItemB;

        this.m_lstItemAContext.Clear();
        lstItemA.ToList().ForEach(a => this.m_lstItemAContext.Add(a));
    }

    #region Control event handlers

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox ddlSender = (ComboBox)sender;
        ItemA itemaSelected = (ItemA)ddlSender.SelectedItem;

        var lstNewItemB = this.m_lstAllItemB.Where(b => b.KeyA == itemaSelected.Key);

        this.m_lstItemBContext.Clear();
        lstNewItemB.ToList().ForEach(b => this.m_lstItemBContext.Add(b));
    }

    private void comboBox2_?(object sender, ?EventArgs e)
    {
        // disable ComboBox  if empty
    }
    #endregion Control event handlers
}

And these are my data classes:

class ItemA
{
    public string Key { get; set; }

    public ItemA(string sKey)
    {
        this.Key = sKey;
    }
}

class ItemB
{
    public string KeyA { get; set; }

    public string Name { get; set; }

    public ItemB(string sKeyA, string sName)
    {
        this.KeyA = sKeyA;
        this.Name = sName;
    }
}

So whenever an item is selected in comboBox1, the appropriate items are supposed to show up in comboBox2. This is working with the current code, though I'm not sure whether my way of re-populating the respective ObservableCollection is 开发者_运维知识库ideal.

What I haven't been able to achieve is actually reacting to changes in the underlying collection of comboBox2, for example to deactivate the control when the list is empty (i.e. when "ccc" is selected in comboBox1).

Of course, I can use an event handler on the CollectionChanged event of the ObservableCollection, and that would work in this example, but in a more complex scenario, where the ComboBox' DataContext might change to a completely different object (and possibly back), that would mean a two-fold dependency - I would always have to not only switch the DataContext, but also the event handlers back and forth. This doesn't seem right to me, but I am probably simply on an entirely wrong track about this.

Basically, what I am looking for is an event firing on the control rather than the underlying list; not the ObservableCollection announcing "my contents have changed", but the ComboBox telling me "something happenend to my items".

What do I need to do, or where do I have to correct my perception of the whole concept ?


Here is the cleaner (perhaps not the much optimized) way to acheive this, keeping your business model untouched, and using ViewModel and XAML only when possible :

View Model :

public class WindowViewModel : INotifyPropertyChanged
{
    private ItemA selectedItem;


    private readonly ObservableCollection<ItemA> itemsA = new ObservableCollection<ItemA>();
    private readonly ObservableCollection<ItemB> itemsB = new ObservableCollection<ItemB>();
    private readonly List<ItemB> internalItemsBList = new List<ItemB>();


    public WindowViewModel()
    {
        itemsA = new ObservableCollection<ItemA> { new ItemA("aaa"), new ItemA("bbb"), new ItemA("ccc") };
        InvokePropertyChanged(new PropertyChangedEventArgs("ItemsA"));

        internalItemsBList = new List<ItemB> { new ItemB("aaa", "a11"), new ItemB("aaa", "a22"), new ItemB("bbb", "b11"), new ItemB("bbb", "b22") };

    }

    public ObservableCollection<ItemA> ItemsA
    {
        get { return itemsA; }
    }

    public ItemA SelectedItem
    {
        get { return selectedItem; }

        set
        {
            selectedItem = value;

            ItemsB.Clear();
            var tmp = internalItemsBList.Where(b => b.KeyA == selectedItem.Key);
            foreach (var itemB in tmp)
            {
                ItemsB.Add(itemB);
            }


            InvokePropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
        }
    }

    public ObservableCollection<ItemB> ItemsB
    {
        get { return itemsB; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }
}

Code Behind :

public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
        DataContext = new WindowViewModel();
    }
}

and XAML :

 <StackPanel>
    <ComboBox Name="comboBox1" ItemsSource="{Binding ItemsA}" DisplayMemberPath="Key" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"  />
    <ComboBox Name="comboBox2" ItemsSource="{Binding ItemsB}" DisplayMemberPath="Name">
        <ComboBox.Style>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="IsEnabled" Value="true"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ItemsB.Count}" Value="0">
                        <Setter Property="IsEnabled" Value="false"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
    </ComboBox>
</StackPanel>

copying-pasting this should work.

Few random thoughts :

1) in WPF, try to always use MVVM pattern and never put code in code-behind files for event handlers. For user actions (like button clicks) use the Commands pattern. For other user actions for which commands are not available, think as much as you can in a "binding-way" : you can do a lot since you can intercept event from the view in VM properties setters (in your example I use the SelectedItem property setter).

2) Use XAML as much as you can. WPF framework provides a very powerful binding and triggers system (in your example, the enabling of combobox don't needs any line of C#).

3) ObservableCollection are made to be exposed by the view model to the view via binding. They are also meant to be used in conjunction with their CollectionChanged event that you can handle in the view model. Take benefit of that (in your example, I play with Observable collection in the VM, where this playing should happen, and any changes in the collection gets reflected in the view via DataBinding).

Hopes this will help !


Basically, what I am looking for is an event firing on the control rather than the underlying list; not the ObservableCollection announcing "my contents have changed", but the ComboBox telling me "something happenend to my items"

if you wanna use MVVM pattern then i would say NO. not the control should give the information, but your viewmodel should.

taking an ObservableCollection is a good step at first. in your specail case i would consider to create just one list with ItemA and i would add a new List property of type ItemB to ItemA.

class ItemA
{
public string Key { get; set; }

public ItemA(string sKey)
{
    this.Key = sKey;
}

public IEnumerable<ItemB> ListItemsB { get; set;}

}

i assume ItemA is the parent?

class ItemB
{

public string Name { get; set; }

public ItemB(string sName)
{
    this.Name = sName;
}
}

you have a collection of ItemA and each ItemA has its own list of depending ItemB.

<ComboBox x:Name="cbo_itemA" ItemsSource="{Binding ListItemA}" DisplayMemberPath="Key"/>
<ComboBox ItemsSource="{Binding ElementName=cbo_itemA, Path=SelectedItem.ListItemsB}"
          DisplayMemberPath="Name" />


Do you need the Keys collection? If not i'd suggest creating it dynamically from the items by grouping via CollectionView:

private ObservableCollection<object> _Items = new ObservableCollection<object>()
{
    new { Key = "a", Name = "Item 1" },
    new { Key = "a", Name = "Item 2" },
    new { Key = "b", Name = "Item 3" },
    new { Key = "c", Name = "Item 4" },
};
public ObservableCollection<object> Items { get { return _Items; } }
<StackPanel>
    <StackPanel.Resources>
        <CollectionViewSource x:Key="ItemsSource" Source="{Binding Items}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Key"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </StackPanel.Resources>
    <StackPanel.Children>
        <ComboBox Name="keyCb" ItemsSource="{Binding Source={StaticResource ItemsSource}, Path=Groups}" DisplayMemberPath="Name"/>
        <ComboBox ItemsSource="{Binding ElementName=keyCb, Path=SelectedItem.Items}" DisplayMemberPath="Name"/>
    </StackPanel.Children>
</StackPanel>

The first ComboBox shows the keys which are generated by grouping by the Key-property, the second binds to the selected item's subitems in the first ComboBox, showing the Name of the item.

Also see the CollectionViewGroup reference, in the fist CB i use the Name in the second the Items.

Of course you can create these key-groups manually as well by nesting items in a key-object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜