【问题标题】:Silverlight databound ComboBox not refreshing when SelectedValue set设置 SelectedValue 时,Silverlight 数据绑定 ComboBox 不刷新
【发布时间】:2011-06-23 07:00:36
【问题描述】:

我的数据绑定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 也是数据绑定的),所以我无法直接访问它来重置绑定/强制重新加载。

【问题讨论】:

    标签: data-binding silverlight-4.0 combobox


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 就我而言,这似乎不是一个解决方案。我在真正的应用程序中,有一个ListBox,并且在每个ListBoxItem 中都是combobox。这些组合框绑定到我后面代码中的集合,并且选定的值是我从列表框中绑定的对象的属性。问题是绑定的对象只知道一个ID(int),它是集合中对象的成员,而不是整个对象。我尝试使用转换器,但无法访问整个集合,并且由于无法直接访问 Combobox,我什至无法读取其 ItemsSource...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2012-01-03
      • 1970-01-01
      相关资源
      最近更新 更多