绑定到列表视图中的WPF组合框(双向)

本文关键字:组合 双向 WPF 列表 视图 绑定 | 更新日期: 2023-09-27 17:50:33

我有这样的问题,必须绑定嵌入在列表视图中的组合框的选定值。在组合框中显示项目没有问题。但是,我希望我有一种方法来规定在单击按钮时组合框应该显示什么(从它包含的项目中)。我想有几个帖子关于这个问题,然而,我不能得到我想要的。这是我的代码。

XAML:

<Grid>
    <StackPanel Orientation="Vertical">
    <ListView 
        x:Name="OptionsListView" 
        ItemsSource="{Binding MyObjectCollection}">
        <ListView.Resources>
            <DataTemplate x:Key="comboBoxTemplate">
                <ComboBox 
                        Margin="0,3" 
                        x:Name="MyTypeComboBox" 
                        SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}">
                    <ComboBoxItem Content="ABC"/>
                    <ComboBoxItem Content="DEF"/>
                    <ComboBoxItem Content="XYZ"/>
                </ComboBox>
            </DataTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Text-Sample" 
                                    Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Combo-Sample" 
                                    Width="100"
                                    CellTemplate="{StaticResource comboBoxTemplate}" />
            </GridView>
        </ListView.View>
    </ListView>
    <Button Click="Button_Click">Click Me!</Button>
    </StackPanel>
</Grid>

c#代码后面:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        OptionsListView.DataContext = this;
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //Something here that dictates what should be displayed in the combo box
    }
    List<MyObject> myObjectCollection = new List<MyObject>();
    public List<MyObject> MyObjectCollection
    {
        get
        {
            myObjectCollection.Add(new MyObject("One"));
            myObjectCollection.Add(new MyObject("Two"));
            return myObjectCollection;
        }
    }
}
public class MyObject : INotifyPropertyChanged
{
    private string _name;
    public MyObject(string name)
    {
        // TODO: Complete member initialization
        this._name = name;
    }
    public string Name
    {
        get
        {
            return _name;
        }
    }
    string selectedType = string.Empty;
    public string SelectedType
    {
        get
        {
            return selectedType;
        }
        set
        {
            selectedType = value;
            this.NotifyChange("SelectedType");
        }
    }
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyChange(params object[] properties)
    {
        if (PropertyChanged != null)
        {
            foreach (string p in properties)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }
    }
    #endregion
}

如果有人能帮我破解这个问题,我会很高兴的。

谢谢Ram

绑定到列表视图中的WPF组合框(双向)

我不确定是否误解了你的问题。我认为你的问题是关于参考的问题。我改变了你的代码一点,它的工作,当点击按钮。

见下面的代码。

XAML:

<ComboBox Margin="0,3" 
    x:Name="MyTypeComboBox" 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=DataContext.Sources}"
    SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}">
</ComboBox>
c#代码:

private Collection<string> sources = new Collection<string>() { "ABC", "DEF", "XYZ" };
public Collection<string> Sources { get { return sources; } }
private void Button_Click(object sender, RoutedEventArgs e)
{
    myObjectCollection[0].SelectedType = Sources[0];
    myObjectCollection[1].SelectedType = Sources[2];
}

foreach (ComboBox c in OptionsListView.Items)
            {
                c.SelectedValue = "Put your value here";
            }

这个应该可以完成工作,如果你有其他对象而不是组合框,你可以添加一个

if (c is ComboBox)

以确保您正在处理正确的对象