当SelectedValue设置时,Silverlight数据绑定ComboBox不刷新

时间:2011-02-08 08:35:22

标签: data-binding silverlight-4.0 combobox

我的数据绑定ComboBox遇到了刷新问题。 当我从代码隐藏中更改SelectedValue时,不会刷新选择。 这是我制作的一个示例,说明了问题所在。我做错了吗?

谢谢!


<navigation:Page x:Class="Views.TestCB" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           DataContext="{Binding RelativeSource={RelativeSource Self}}"
           Title="TestCB Page">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <ComboBox ItemsSource="{Binding AllItems, Mode=TwoWay}" Grid.Row="0" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" DisplayMemberPath="Key" SelectedValuePath="Value" SelectedValue="{Binding SelectedValue, Mode=TwoWay}"/>
        <Button Content="Change" Click="Button_Click" Grid.Row="1" />
        <Button Content="Display" Click="Button_Click_1" Grid.Row="2" />
    </Grid>
</navigation:Page>

public partial class TestCB : Page, INotifyPropertyChanged
{
    public TestCB()
    {
        InitializeComponent();
    }

    private Random r = new Random();

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    private ObservableCollection<MyClass> allItemsField = new ObservableCollection<MyClass>();
    public ObservableCollection<MyClass> AllItems
    {
        get
        {
            return this.allItemsField;
        }

        set
        {
            if (this.allItemsField != value)
            {
                this.allItemsField = value;
                this.OnPropertyChanged("AllItems");
            }
        }
    }

    private MyClass selectedItemField = null;
    public MyClass SelectedItem
    {
        get
        {
            return this.selectedItemField;
        }

        set
        {
            if (this.selectedItemField != value)
            {
                this.selectedItemField = value;
                this.OnPropertyChanged("SelectedItem");
            }
        }
    }

    private int selectedValueField;
    public int SelectedValue
    {
        get
        {
            return this.selectedValueField;
        }

        set
        {
            if (this.selectedValueField != value)
            {
                this.selectedValueField = value;
                this.OnPropertyChanged("SelectedValue");
            }
        }
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.SetCombo();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.SetCombo();
        this.SelectRandom();
    }

    private void SetCombo()
    {
        this.AllItems.Clear();

        int generated = 0;
        while (generated < 10)
        {
            int val = r.Next(100);
            string key = string.Format("Key #{0}", val);

            if (!this.AllItems.Any(e => e.Key == key))
            {
                this.AllItems.Add(new MyClass { Key = key, Value = val });
                generated++;
            }
        }
    }

    private void SelectRandom()
    {
        var tmp = this.AllItems[r.Next(this.AllItems.Count)];
        this.SelectedValue = tmp.Value;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(this.SelectedItem.Key);
    }
}

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    private int valueField;
    public int Value
    {
        get
        {
            return this.valueField;
        }

        set
        {
            if (this.valueField != value)
            {
                this.valueField = value;
                this.OnPropertyChanged("Value");
            }
        }
    }

    private string keyField = string.Empty;
    public string Key
    {
        get
        {
            return this.keyField;
        }

        set
        {
            if (this.keyField != value)
            {
                this.keyField = value;
                this.OnPropertyChanged("Key");
            }
        }
    }
}

编辑:请注意,在我的计划中,ComboBoxListBoxItem的一部分(ListBox也是数据绑定),所以我可以' t直接访问它以重置绑定/强制重新加载。

2 个答案:

答案 0 :(得分:2)

我终于使用了这里找到的组合框:http://forums.lhotka.net/forums/p/9786/45971.aspx 它纠正了我的SelectedValue绑定问题。

答案 1 :(得分:0)

您是否尝试过设置SelectedItemSelectedValue有一些know issues