数据库ComboBox在值更改后不会立即刷新

时间:2011-12-22 17:51:14

标签: silverlight data-binding combobox

我有一个包含2个项目的ComboBox,其内容可以数据绑定为2个依赖项属性(FooBar)。 我有一个增加FooBar的按钮。 当我按下此按钮时,我在输出窗口中看到FooBar确实发生了变化。在绑定上设置断点(仅限SL 5)也证明了这一点。 但是ComboBox上显示的值不会更新! 它仅在我单击ComboBox或通过[TAB]和[DOWN]更改所选项目时更新。

我甚至尝试在值更新后在我的ComboBox上调用UpdateLayout(),但无济于事。

这是我要测试的代码。

代码背后:

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace ComboBoxBindingTest
{
    public partial class MainPage : UserControl
    {
        public int Foo
        {
            get { return (int)GetValue(FooProperty); }
            set { SetValue(FooProperty, value); }
        }
        public static readonly DependencyProperty FooProperty =
            DependencyProperty.Register("Foo", typeof(int), typeof(MainPage), new PropertyMetadata(0));

        public int Bar
        {
            get { return (int)GetValue(BarProperty); }
            set { SetValue(BarProperty, value); }
        }
        public static readonly DependencyProperty BarProperty =
            DependencyProperty.Register("Bar", typeof(int), typeof(MainPage), new PropertyMetadata(0));

        public MainPage()
        {
            Foo = 0;
            Bar = 0;
            InitializeComponent();
            DataContext = this;
        }

        private void Step()
        {
            Foo++;
            Bar++;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Step();
            Debug.WriteLine("Foo: {0}", Foo);
            Debug.WriteLine("Bar: {0}", Bar);
        }
    }
}

XAML:

<UserControl x:Class="ComboBoxBindingTest.MainPage"
             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"
             d:DesignHeight="138"
             d:DesignWidth="188">

    <StackPanel Background="White">
        <ComboBox Width="120"
                  Height="23"
                  HorizontalAlignment="Center"
                  VerticalAlignment="Center">
            <ComboBoxItem Content="{Binding Foo}"
                          IsSelected="True" />
            <ComboBoxItem Content="{Binding Bar}" />
        </ComboBox>
        <Button Content="Step"
                Width="75"
                Height="23"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Click="Button_Click" />
    </StackPanel>
</UserControl>

我做错了什么,这里?

1 个答案:

答案 0 :(得分:0)

Silverlight Combobox仅在您选择更改时更新其文本。您可以创建自定义Combobox,它可以在不同的事件上更改它的文本。或者(作为一个不太好的解决方法),您可以重新选择当前选择的项目:

private void button1_Click(object sender, RoutedEventArgs e)
{
    Step();
    var selectedItem = combobox.SelectedItem;
    combobox.SelectedItem = null;
    combobox.SelectedItem = selectedItem;
    Debug.WriteLine("Foo: {0}", Foo);
    Debug.WriteLine("Bar: {0}", Bar);
}

编辑:另一种解决方案
如果您没有手动添加组合框项目但将组合框绑定到viewmodel集合,则可以获得相同的结果(尽管代码与您现在的代码完全不同)

项目的ViewModel:

public class Item : INotifyPropertyChanged
{
    private int foo;
    public int Foo { get { return foo; } set { foo = value; FirePropertyChanged("Foo"); } }

    public event PropertyChangedEventHandler PropertyChanged;

    public void FirePropertyChanged(string prop)
    {
        if (PropertyChanged != null) PropertyChanged(this, new  ropertyChangedEventArgs(prop));
    }
}

代码背后:

public MainPage()
{

    InitializeComponent();            
    Items = new List<Item> {new Item {Foo = 0}, new Item {Foo = 1}};
    DataContext = this;
    combobox.SelectedItem = Items.First();
}

public List<Item> Items { get; set; }

private void Step()
{
    foreach (var item in Items)
    {
        item.Foo++;
    }
}

XAML

<ComboBox Width="120" x:Name="combobox"
        Height="23" 
        HorizontalAlignment="Center"
        VerticalAlignment="Center" ItemsSource="{Binding Items}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Foo}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>               
</ComboBox>