2016-02-05 59 views
1

我很困難更新WPF中的綁定數據時遇到問題。 基本上,我在我的應用程序中有一些數據,當屬性更改時不更新,我無法修復它。如何正確地將數據綁定到列表框,以便它會更新[C#/ WPF/MVVM]

在我的應用我試圖使用MVVM模式,所以與特性X模型,Y:

public class Сross : INotifyPropertyChanged 
{ 
    private double x; 
    private double y; 

    public double X 
    { 
     get { return x; } 
     set 
     { 
      x = value; 
      RaisePropertyChanged("X"); 
     } 
    } 

    public double Y 
    { 
     get { return y; } 
     set 
     { 
      y = value; 
      RaisePropertyChanged("Y"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

...有一個視圖模型,其中包含十字的一個ObservableCollection:

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    Crosses = new ObservableCollection<Cross>() 
    { 
     new Cross() 
     { 
      X = 300, 
      Y = 200 
     }, 
     new Cross() 
     { 
      X = 400, 
      Y = 300 
     } 
    }; 

    private ObservableCollection<Cross> crosses; 
    public ObservableCollection<Cross> Crosses 
    { 
     get 
     { 
      return crosses; 
     } 
     set 
     { 
      crosses = value; 
      RaisePropertyChanged("Crosses"); 
     } 
    } 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

...而且有一種說法,那裏有一個列表框,數據綁定到這個的ObservableCollection十字架。

<Page x:Class="CharMaker.App.MainPageView"> 
    <Page.DataContext> 
     <local:MainWindowViewModel /> 
    </Page.DataContext> 

    .... 

    <ListBox> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas IsItemsHost="True" Background="#01FFFFFF" /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 

     <ListBox.ItemsSource> 
      <Binding Path="Crosses" Mode="TwoWay" /> 
     </ListBox.ItemsSource> 

     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
        <Setter Property="Canvas.Left" Value="{Binding X, Mode=TwoWay}" /> 
        <Setter Property="Canvas.Top" Value="{Binding Y, Mode=TwoWay}" /> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
</Page> 

此代碼的工作,這樣當你在ListBox場移動十字項(其中有基於拇指控制一個DataTemplate),它是Canvas.Left(或頂部)正在改變,它更新X和Y交叉項目的屬性,反之亦然。

<DataTemplate DataType="{x:Type model:Cross}"> 
          <Thumb DragDelta="Thumb_DragDelta" 
          IsEnabled="{Binding IsSelected,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"> 
           <Thumb.Template> 
            <ControlTemplate TargetType="Thumb"> 
             <Canvas Margin="0"> 
              <Line X1="-5" X2="5" Y1="-5" Y2="5" Stroke="#FF2E61AB" StrokeThickness="1.5" 
              x:Name="FirstLine" /> 

              <Line X1="-5" X2="5" Y1="5" Y2="-5" Stroke="#FF2E61AB" StrokeThickness="1.5" 
              x:Name="SecondLine" /> 
             </Canvas> 
             <ControlTemplate.Triggers> 
              <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True"> 
               <Setter TargetName="FirstLine" Property="Stroke" Value="Red"/> 
               <Setter TargetName="SecondLine" Property="Stroke" Value="Red"/> 
              </DataTrigger> 
             </ControlTemplate.Triggers> 
            </ControlTemplate> 
           </Thumb.Template> 
          </Thumb> 
         </DataTemplate> 

的問題是,列表框被加載後(和十字架在適當的位置出現在屏幕上),它們不改變X和Y性的判定在視圖模型,因爲我移動它們。如果我在ObservableCollection Crosses中更改X和Y,則它們不會在屏幕上移動。 值得一提的是,我做了與文本框進行測試列表框,這也與X鍵,十字收集的Y屬性:

<ListBox Grid.Row="2" Grid.ColumnSpan="4" ItemsSource="{Binding Crosses}"> 
    <ListBox.Resources> 
     <DataTemplate DataType="{x:Type model:Cross}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="1*"/> 
        <ColumnDefinition Width="1*"/> 
       </Grid.ColumnDefinitions> 

       <TextBox Grid.Column="0" Text="{Binding X, Mode=TwoWay}" /> 
       <TextBox Grid.Column="1" Text="{Binding Y, Mode=TwoWay}" /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.Resources> 
</ListBox> 

和有趣的是,在這個ListBox中的數據綁定的作品就好了! 當我移動十字架時,文本框中的值發生變化,當文本框中的值發生更改時,十字移動到位置。

但是在同一時間(如果我暫停應用程序),我發現ViewModel中的Observable Collection Crosses中的值與第一次加載時的值相同。

請幫我弄清楚是什麼問題! 提前謝謝!

+0

當X,Y被更改時,調試視圖會顯示什麼內容?視圖中是否有綁定錯誤? –

+0

什麼是你的Cross的DataTemplate模樣? – Peter

+0

@JeongKim Debug視圖顯示,即使在移動十字之後,X和Y仍然是初始化期間加載的值。但在屏幕上,我看到它們被移動,帶有座標的TextBox顯示實際值。 – Lark3D

回答

0

經過大量的時間調試這個,似乎我發現了這個問題。

我有一個DataContext設置爲MainViewModel的頁面,並且此頁面是MainWindow的子元素,它也將DataContext屬性設置爲MainViewModel。 我沒有意識到,Page和MainWindow都創建了自己的MainViewModel實例。 所以我有兩個實例,而一個正在工作和更新,因爲它應該,另一個只是讓我困惑。

愚蠢的錯誤,我知道。 無論如何感謝您的幫助。

相關問題