wpf - ItemsControl 包含 ItemTemplate 中绑定(bind)的 ComboBox

标签 wpf binding combobox

我刚刚陷入了将 ItemsControl 中的集合与包含有界 ComboBox 的 ItemTeplate 绑定(bind)的问题。

在我的场景中,我需要“生成”表单,其中包括集合中每个项目的文本框和组合框,并让用户更新项目。我可以使用 DataGrid 来实现此目的,但我希望在编辑模式下查看所有行,因此我将 ItemsControl 与自定义 ItemTemplate 一起使用。

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

谢谢,翁德雷

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

最佳答案

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

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

关于wpf - ItemsControl 包含 ItemTemplate 中绑定(bind)的 ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987781/

相关文章:

c# - 如何在 WPF 的组合框中实现多选

java - java中自动完成组合框的问题

wpf - 工具栏按钮样式被忽略?

.net - WPF 应用程序失去触摸功能

.net - WPF MVVM Prism 应用程序中的内存泄漏

c# - 带格式字符串的 MvvMCross 绑定(bind)

c# - 获取 ComboBox 的 SelectedItem (MVVM)

c# - 如何将 WPF 窗口渲染到 DirectX 缓冲区中?

python - coverage.py 是否正确显示 python 绑定(bind)的测试覆盖率?

ios - "The native class hasn' t been loading"错误与我的绑定(bind)