数据绑定ComboBox在值更改后未立即刷新
问题描述:
我有一个ComboBox,其内容是数据绑定到2个依赖项属性(Foo
和Bar
)的2个项目。 我有一个按钮,它增加了Foo
和Bar
。 当我按下此按钮时,我在输出窗口中看到Foo
和Bar
的确已发生变化。在绑定上设置断点(仅限SL 5)也证明了这一点。 但ComboBox上显示的值未更新! 只有当我点击组合框或通过[TAB]和[DOWN]更改所选项目时,它才会更新。数据绑定ComboBox在值更改后未立即刷新
我甚至试图在值更新后在我的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>
我在做什么错了,在这里?
答
Silverlight组合框仅在您选择已更改时更新文本。您可以创建自定义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);
}
编辑:另一种解决方案
如果没有人工添加组合框项目,但组合框绑定到视图模型集合就可以达到同样的效果(虽然该代码将是你有什么,现在完全不同)
视图模型的项目:
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>
谢谢您的anwser!明天我会试一试。 – Rodolphe 2011-12-22 19:53:21
对不起,这是一段时间...我现在使用'ObservableCollection'并且一切正常。非常感谢! – Rodolphe 2012-02-03 15:33:06