2011-12-22 127 views
0

我有一個ComboBox,其內容是數據綁定到2個依賴項屬性(FooBar)的2個項目。 我有一個按鈕,它增加了FooBar。 當我按下此按鈕時,我在輸出窗口中看到FooBar的確已發生變化。在綁定上設置斷點(僅限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> 

我在做什麼錯了,在這裏?

回答

0

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> 
+0

謝謝您的anwser!明天我會試一試。 – Rodolphe 2011-12-22 19:53:21

+0

對不起,這是一段時間...我現在使用'ObservableCollection'並且一切正常。非常感謝! – Rodolphe 2012-02-03 15:33:06

相關問題