ItemsControl包含ItemTemplate中绑定的ComboBox

时间:2012-01-24 13:40:48

标签: wpf binding combobox

我只是遇到了一个问题,要将ItemsControl中的集合与包含有界ComboBox的ItemTeplate绑定。

在我的场景中,我需要“生成”表单,其中包含集合中每个项目的文本框和组合框,并允许用户更新项目。我可以使用DataGrid,但我想在编辑模式下看到所有行,所以我使用ItemsControl和自定义ItemTemplate。

编辑文本框是可以的,但是当您尝试更改任何ComboBox时,其他行中的所有其他ComboBox也会更改。 这是一个错误或功能吗?

谢谢,Ondrej

Window.xaml

<Window x:Class="ComboInItemsControlSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="480" Width="640">
<Window.Resources>

    <CollectionViewSource x:Key="cvsComboSource"
        Source="{Binding Path=AvailableItemTypes}" />

    <DataTemplate x:Key="ItemTemplate">
        <Border BorderBrush="Black" BorderThickness="0.5" Margin="2">
            <Grid Margin="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>

                <TextBox Grid.Column="0" Text="{Binding Path=ItemValue}" />
                <ComboBox Grid.Column="2"
                          SelectedValue="{Binding Path=ItemType}"
                          ItemsSource="{Binding Source={StaticResource cvsComboSource}}"
                          DisplayMemberPath="Name"
                          SelectedValuePath="Value" />
            </Grid>
        </Border>
    </DataTemplate>

</Window.Resources>
<Grid>

    <ItemsControl ItemsSource="{Binding Path=SampleItems}"
                  ItemTemplate="{StaticResource ItemTemplate}"
                  Margin="10" />

</Grid>

Window.xaml.cs

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        DataContext = new ViewModel();
    }
}

public class ViewModel
{
    public ViewModel()
    {
        SampleItems = new List<SampleItem> {
            new SampleItem { ItemValue = "Value 1" },
            new SampleItem { ItemValue = "Value 2" },
            new SampleItem { ItemValue = "Value 3" }
        };

        AvailableItemTypes = new List<SampleItemType> {
            new SampleItemType { Name = "Type 1", Value = 1 },
            new SampleItemType { Name = "Type 2", Value = 2 },
            new SampleItemType { Name = "Type 3", Value = 3 },
            new SampleItemType { Name = "Type 4", Value = 4 }
        };
    }

    public IList<SampleItem> SampleItems { get; private set; }
    public IList<SampleItemType> AvailableItemTypes { get; private set; }
}

public class SampleItem : ObservableObject
{
    private string _itemValue;
    private int _itemType;

    public string ItemValue
    {
        get { return _itemValue; }
        set { _itemValue = value; RaisePropertyChanged("ItemValue"); }
    }
    public int ItemType
    {
        get { return _itemType; }
        set { _itemType = value; RaisePropertyChanged("ItemType"); }
    }
}

public class SampleItemType : ObservableObject
{
    private string _name;
    private int _value;

    public string Name
    {
        get { return _name; }
        set { _name = value; RaisePropertyChanged("Name"); }
    }
    public int Value
    {
        get { return _value; }
        set { _value = value; RaisePropertyChanged("Value"); }
    }
}

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName) {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

图片

here you can see the result on picture

2 个答案:

答案 0 :(得分:7)

我相信这是因为你绑定了跟踪当前项目的CollectionViewSource。请尝试直接绑定到您的列表,而不会跟踪当前项目

<ComboBox Grid.Column="2"
          SelectedValue="{Binding Path=ItemType}"
          DisplayMemberPath="Name"
          SelectedValuePath="Value"
          ItemsSource="{Binding RelativeSource={
              RelativeSource AncestorType={x:Type ItemsControl}}, 
              Path=DataContext.AvailableItemTypes}" />

答案 1 :(得分:1)

虽然每排都有一个组合框,但它并不认为这些组合框是分开的。即它们都使用相同的集合和相同的selectedValue,所以当一个值中的值发生变化时,它会在所有变量中发生变化。

解决此问题的最佳方法是将SampleItemType集合添加为SampleItem模型的属性,然后将组合框绑定到该属性。