2012-07-17 105 views
1

我知道這已被問了幾次,但沒有答案適合我。強制綁定UI組件刷新

我已經有了TreeView中的這個元素列表,當點擊時它們在一個面板中顯示它們的參數(包括texboxes,labels,combos和其他組件)。我還有一個按鈕,允許將數據從組合框中選擇的一個元素複製到數據面板上當前顯示的元素(「從...複製數據」)。

面板中的所有數據都綁定到類及其屬性,並且樹視圖綁定到這些對象的集合。

問題是,當我按下複製按鈕時(即將數據從對象A複製到當前對象),更改不會被反映出來。但是,如果我更改要顯示的元素(單擊TreeView中的另一個對象),然後返回到已更改的對象,所做的更改就在那裏。所以它實際上正在改變實際的數據,但不會刷新數據綁定。

奇怪的是,如果我按下複製按鈕兩次,確實反映了更改。

這可能是原因,我將如何解決它?

這是一個應該被更新(我只發佈一個因爲有很多,其中沒有工作),控制的XAML例子複製按鈕和樹視圖:

Copiar DATOS德爾protocolo: Copiar

<TreeView SelectedItemChanged="TvProtocolosSelectedItemChanged" Margin="10,5" Name="tvProtocolos" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="200" Height="292" MinWidth="0"> 
        <TreeView.ItemContainerStyle> 
         <Style TargetType="TreeViewItem"> 
          <Setter Property="IsEnabled" Value="{Binding Path=Activo}"/> 
          <Setter Property="IsExpanded" Value="False"/> 
         </Style> 
        </TreeView.ItemContainerStyle> 
        <TreeView.ItemTemplate> 
         <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> 
          <StackPanel Orientation="Horizontal"> 
           <Image Source="imagenes\file_icon.gif" Margin="0,0,5,0" /> 
           <TextBlock Text="{Binding Path=Name}" ></TextBlock> 
          </StackPanel> 
         </HierarchicalDataTemplate> 
        </TreeView.ItemTemplate> 

       </TreeView> 

這是代碼隱藏文件:

public partial class NewXP2 : Window 
    { 
     private const int nProtocolos = 100; 
     private Experiencia2 _exp2Class = new Experiencia2(nProtocolos); 
     private readonly TreeView _tvProtocolos; 


     public NewXP2() 
     { 
InitializeComponent(); 
_tvProtocolos.ItemsSource = _expClass.Protocolos; 
xpControlsPanel.DataContext = _exp2Class.GetProtocolo(0); 
      for(int i=0;i<nProtocolos;i++) 
       copyFromCombo.Items.Add("Protocolo " + (i+1)); 
      copyFromCombo.SelectedIndex = 0; 
} 

     private void CopyfromButtonClick(object sender, RoutedEventArgs e) 
     { 
      int protIndex = copyFromCombo.SelectedIndex; 

      int indiceProtocolo = 0; 
      if (_tvProtocolos == null) 
       return; 

      var g = _tvProtocolos.SelectedItem as Composite; 

      _listaData = GetData(); 
      _tvProtocolos.ItemsSource = _listaData; 
      TreeViewItem tvi; 
      if (g != null) 
      { 
       tvi = _tvProtocolos.ItemContainerGenerator.ContainerFromIndex(g.Indice) as TreeViewItem; 

       if (tvi != null) 
       { 
        tvi.IsSelected = true; 
       } 
       indiceProtocolo = g.Indice; 
      } 

      _exp2Class.SetProtocolo(indiceProtocolo, _exp2Class.Protocolos[protIndex]); 

     } 

這必將給面板類(文本框)

public class ProtocoloExp2: ISerializable, IDataErrorInfo 
    { 
public ProtocoloExp2(int idx) 
     { 
      IndiceProtocolo = idx; 
      IndiceVisual = idx + 1; 
     } 
     public float TimeToShowTarget { get; set; } 

     (...) 
} 
+1

聽起來像是在INotifyPropertyChanged實現中破壞了某些東西,但沒有相關的代碼示例很難說明。 – Alex 2012-07-17 12:46:10

+0

我應該發佈什麼樣的應用程序?我沒有在任何類中使用該接口。我想我可能會在這裏失去一些東西... – 2012-07-17 12:48:21

+0

發佈問題的最小可能repro。 – 2012-07-17 12:49:57

回答

2

檢查"How to: Implement the INotifyPropertyChanged Interface"出來。並考慮使用ObservableCollection作爲您的列表。 INotifyPropertyChanged接口的實現是wpf控件視圖模型的一個要求,它基本上允許視圖在數據更改時自行刷新。

+0

我會檢查一下,儘管我的綁定到目前爲止工作完美,在兩個方向。 – 2012-07-17 13:03:29

+1

如果您在更改屬性窗體代碼後顯示「從...複製數據...」按鈕單擊說明時,則不行。 – Rafal 2012-07-17 13:08:46