开发者

Binding to a WPF combo box in a list view (2-way)

I have this problem of having to bind the selected value of a combo box embedded inside a list view. I have no trouble in displaying items in the combo box. However, I wish I had a way to dictate what the combo box should display (from among the items that it holds) on the click of a button. I think there are several posts on this issue, however, I am not able to get exactly what I want. Here is my code.

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>
        开发者_Go百科</ListView.View>
    </ListView>
    <Button Click="Button_Click">Click Me!</Button>
    </StackPanel>
</Grid>

C# Code Behind:

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
}

I would be glad if someone could help me crack this..

Thanks Ram


I'm not sure if I misunderstanding your question. I think your issue is about the reference issue. I changed your code a little and it works when click on the button.

See the code below.

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# code:

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];
}


How about

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

This should do the work, if you have other objects than comboboxes inside you can add a

if (c is ComboBox)

to ensure that you are working on the right object

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜