Data binding Silverlight数据绑定组合框在SelectedValue设置时不刷新

Data binding Silverlight数据绑定组合框在SelectedValue设置时不刷新,data-binding,silverlight-4.0,combobox,Data Binding,Silverlight 4.0,Combobox,我的数据绑定组合框出现刷新问题。 当我从代码隐藏更改SelectedValue时,选择不会刷新。 下面是我制作的一个示例,它说明了这个问题。我做错什么了吗 谢谢 public分部类TestCB:Page,INotifyPropertyChanged { 公共测试CB() { 初始化组件(); } 私有随机r=新随机(); 公共事件属性更改事件处理程序属性更改; 受保护的void OnPropertyChanged(字符串名称) { PropertyChangedEventHandler处

我的数据绑定组合框出现刷新问题。 当我从代码隐藏更改
SelectedValue
时,选择不会刷新。 下面是我制作的一个示例,它说明了这个问题。我做错什么了吗

谢谢




public分部类TestCB:Page,INotifyPropertyChanged
{
公共测试CB()
{
初始化组件();
}
私有随机r=新随机();
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
private ObservableCollection allItemsField=新ObservableCollection();
公共可观测集合项目
{
得到
{
返回此.allItemsField;
}
设置
{
if(this.allItemsField!=值)
{
this.allItemsField=值;
本协议项下的不动产变更(“AllItems”);
}
}
}
private MyClass selectedItemField=null;
公共MyClass SelectedItem
{
得到
{
返回此字段。选择编辑字段;
}
设置
{
if(this.selectedItemField!=值)
{
this.selectedItemField=值;
此.OnPropertyChanged(“SelectedItem”);
}
}
}
private int selectedValueField;
公共整数选择值
{
得到
{
返回此。selectedValueField;
}
设置
{
if(this.selectedValueField!=值)
{
this.selectedValueField=值;
此.OnPropertyChanged(“SelectedValue”);
}
}
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
this.SetCombo();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
this.SetCombo();
这个。SelectRandom();
}
私有void SetCombo()
{
这个.AllItems.Clear();
int=0;
而(生成时间<10)
{
int val=r.Next(100);
string key=string.Format(“key{0}”,val);
如果(!this.AllItems.Any(e=>e.Key==Key))
{
Add(新的MyClass{Key=Key,Value=val});
生成++;
}
}
}
私有void SelectRandom()
{
var tmp=this.AllItems[r.Next(this.AllItems.Count)];
this.SelectedValue=tmp.Value;
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
MessageBox.Show(this.SelectedItem.Key);
}
}
公共类MyClass:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
私人投资领域;
公共整数值
{
得到
{
返回此.valueField;
}
设置
{
if(this.valueField!=值)
{
this.valueField=值;
本协议项下的不动产变更(“价值”);
}
}
}
private string keyField=string.Empty;
公共字符串密钥
{
得到
{
返回这个.keyField;
}
设置
{
if(this.keyField!=值)
{
this.keyField=值;
此项。OnPropertyChanged(“密钥”);
}
}
}
}

编辑:请注意,在我的程序中,
组合框
列表框项目
的一部分(列表框也被数据绑定),因此我无法直接访问它来重置绑定/强制重新加载。

您尝试过设置
SelectedItem
SelectedValue有一些。

我最后使用了这里的组合框:
它纠正了我的SelectedValue绑定问题。

在我的情况下,它似乎不是一个解决方案。在实际应用程序中,有一个ListBox,每个ListBoxItem中都有一个combobox。这些组合框绑定到my code behind中的一个集合,所选值是从列表框绑定的my object的属性。问题是绑定对象只知道一个ID(int),它是集合中对象的一个成员,而不知道整个对象。我尝试使用转换器,但我无法访问整个集合,而且由于我无法直接访问Combobox,我甚至无法读取它的ItemsSource。。。
<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");
            }
        }
    }
}