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

Ram*_*Ram 4 c# data-binding wpf

我有这个问题,必须绑定列表视图中嵌入的组合框的选定值.我在组合框中显示项目没有任何问题.但是,我希望我能通过点击按钮来指示组合框应该显示的内容(从它保存的项目中).我认为在这个问题上有几个帖子,但是,我无法得到我想要的.这是我的代码.

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>
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢拉姆

How*_*ard 5

我不确定我是否误解了你的问题.我认为您的问题与参考问题有关.我稍微改变了你的代码,点击按钮就可以了.

请参阅下面的代码.

XAML:

<ComboBox Margin="0,3" 
    x:Name="MyTypeComboBox" 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=DataContext.Sources}"
    SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}">
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

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];
}
Run Code Online (Sandbox Code Playgroud)