2016-02-05 31 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实例。 所以我有两个实例,而一个正在工作和更新,因为它应该,另一个只是让我困惑。

愚蠢的错误,我知道。 无论如何感谢您的帮助。

相关问题