0

我有一个 wpf 应用程序,当我使用 Observablecollect 通过 UI 更改值时,我想更新我的列表视图。但我没有得到我所期望的。当我更改值时,我不会更新我的列表视图。查看代码(Xaml)

<UserControl x:Class="DataWatch.View.CompareData"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ViewModels="clr-namespace:DataWatch.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="340" d:DesignWidth="600">
<UserControl.DataContext>
    <ViewModels:CompareViewModel/>
</UserControl.DataContext>
<Grid>
    <ListView HorizontalAlignment="Left" Name="comparelistview" VerticalAlignment="Top" Width="600" Height="340" ItemsSource="{Binding DisplayData}">
        <ListView.View>

            <GridView>
                <GridViewColumn Width="150" Header="Key"
                  DisplayMemberBinding="{Binding Path=Key}" />
                <GridViewColumn Width="150" Header="Project Data" 
                  DisplayMemberBinding="{Binding Path=ProjectData}" />
                <GridViewColumn Width="150" Header="Import Data" 
                  DisplayMemberBinding="{Binding Path=ImportData}"/>
                <GridViewColumn Width="150" Header="State" 
                  DisplayMemberBinding="{Binding Path=State}"/>


            </GridView>
        </ListView.View>
    </ListView>

</Grid>

视图模型

    namespace DataWatch.ViewModel
   {

     public class CompareViewModel
     {
        private ObservableCollection<CompareDiplayData> _displayData;

         public CompareViewModel()
        {
              _displayData = new ObservableCollection<CompareDiplayData>();

        }
                public ObservableCollection<CompareDiplayData> DisplayData
        {
            get { return _displayData; } 
        }
    }

模型:

    namespace DataWatch.Model
   {
     public class CompareDiplayData : INotifyPropertyChanged
     {
        private string _key;
        public string Key
        {
            set
            {
                _key = value;
                this.Changed("Key");
            }
            get
            {
                return _key;
            }
        }
        private string _projectData;

        public string ProjectData
        {
            set
            {
                _projectData = value;
                this.Changed("ProjectData");
            }
            get
            {
                return _projectData;
            }
        }
        private string _importData;

        public string ImportData
        {
            set
            {
                _importData = value;
                this.Changed("ImportData");
            }
            get
            {
                return _importData;
            }
        }
        private string _state;
        public string State
        {
            set
            {
                _state = value;
                this.Changed("State");
            }
            get
            {
                return _state;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void Changed(string PropertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}

当我更改 displayData 中的值时,但 Listview 不会更新数据。组合框视图控件

<UserControl x:Class="DataWatch.View.SelectPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ViewModels="clr-namespace:DataWatch.ViewModel"
         xmlns:AttachProperty="clr-namespace:DataWatch.AttachedProperty"
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="600">
<UserControl.DataContext>
    <ViewModels:CompareViewModel/>
</UserControl.DataContext>
<Grid Name="good">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150">
        </ColumnDefinition>
        <ColumnDefinition Width="300">
        </ColumnDefinition>
        <ColumnDefinition Width="150">

        </ColumnDefinition>
    </Grid.ColumnDefinitions>

    <ComboBox Height="23" HorizontalAlignment="Left"  Grid.Column="0" Grid.Row="0" Margin="15,4,6,4" x:Name="KeyComboBox" VerticalAlignment="Top" Width="120"  Text="Choose Key" AttachProperty:SelectionBehavior.SelectionChanged="{Binding SelectKeyCmd}"  SelectedItem="{Binding SelectedKey, Mode=TwoWay}" ItemsSource="{Binding KeyComboboxItem}"
      IsEditable="true"  IsReadOnly="true" 
      IsDropDownOpen="True" StaysOpenOnEdit="True"/>
    <ComboBox Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" Margin="90,4,6,4" Name="compareitemcomBox" VerticalAlignment="Top" Width="120"  Text="Compare Item" AttachProperty:SelectionBehavior.SelectionChanged="{Binding SelectKeyCmd}" SelectedItem="{Binding SelectedComparedData, Mode=TwoWay}" ItemsSource="{Binding CompareComboboxItem}"
      IsEditable="true"  IsReadOnly="true" 
      IsDropDownOpen="True" StaysOpenOnEdit="True"/>
    <ComboBox Height="23" HorizontalAlignment="Left" Grid.Column="2" Grid.Row="0" Margin="15,4,6,4" Name="display" VerticalAlignment="Top" Width="120"  Text="Choose State" 
      IsEditable="true"  IsReadOnly="true" 
      IsDropDownOpen="True" StaysOpenOnEdit="True"/>

</Grid>

附加属性

 public class SelectionBehavior
{
    public static DependencyProperty SelectionChangedProperty =
       DependencyProperty.RegisterAttached("SelectionChanged",
       typeof(ICommand),
       typeof(SelectionBehavior),
       new UIPropertyMetadata(SelectionBehavior.SelectedItemChanged));
    public static void SetSelectionChanged(DependencyObject target, ICommand value)
    {
        target.SetValue(SelectionBehavior.SelectionChangedProperty, value);
    }
    private static void SelectedItemChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Selector element = target as Selector;
        if (element == null) throw new InvalidOperationException("This behavior can be attached to Selector item only.");
        if ((e.NewValue != null) && (e.OldValue == null))
        {
            element.SelectionChanged += SelectionChanged;
        }
        else if ((e.NewValue == null) && (e.OldValue != null))
        {
            element.SelectionChanged -= SelectionChanged;
        }
    }
    private static void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        UIElement element = (UIElement)sender;
        ICommand command = (ICommand)element.GetValue(SelectionBehavior.SelectionChangedProperty);
        command.Execute(((Selector)sender).SelectedValue);
    }

}

视图模型

public class CompareViewModel 
{
    private readonly ICommand _selectKeyCmd;
    private List<string> _pro_Property;
    private List<string> _imp_Property;
    private string selectedKey;
    private ObservableCollection<string> compareComboboxItem;
    private DataTable _dt;
   private CompareDiplayData domObject;
    private ObservableCollection<CompareDiplayData> _displayData;



    public CompareViewModel()
    {
                               _displayData = new ObservableCollection<CompareDiplayData>();
        _selectKeyCmd = new RelayCommand(ComboboxChanged, ComboboxIsChanged);

    }
    private void ReslutData()
    {_displayData =null;

        if (SelectedKey != null && SelectedComparedData != null)
        {
            foreach (DataRow pdr in _projectDt.Rows)
            {

                CompareDiplayData cdd = new CompareDiplayData();
                foreach (DataRow idr in _importDt.Rows)
                {

                    if (pdr[SelectedKey].ToString() == idr[SelectedKey].ToString())
                    {
                        cdd.Key = pdr[SelectedKey].ToString();
                        cdd.ProjectData = pdr[SelectedComparedData].ToString();
                        cdd.ImportData = idr[SelectedComparedData].ToString();
                        if (pdr[SelectedComparedData].ToString() == idr[SelectedComparedData].ToString())
                            cdd.State = "Match";
                        else
                            cdd.State = "Mismatch";
                        _displayData.Add(cdd);
                    }

                }


            }
        }
    }
 public ObservableCollection<CompareDiplayData> DisplayData
    {
        get { return _displayData; } 
    }
 public ICommand SelectKeyCmd
    {
        get { return _selectKeyCmd; }
    }
  private void ComboboxChanged(object obj)
    {
        ReslutData();
    }
    private bool ComboboxIsChanged(object obj)
    {
        return true;
    }`}`
4

1 回答 1

0

您正在更新Observablecollection此处的参考,因此需要将其作为PropertyChange. 为了解决这个问题,您还必须INotifyPropertyChanged在 ViewModel 上实现,并编写 DisplayData 的设置器并为其引发属性更改。

        public ObservableCollection<CompareDiplayData> DisplayData
        {
            get { return _displayData; } 
            set{_displayData = value;
               this.Changed("DisplayData"); 
        }

并在您的 ReslutData 函数中,而不是更新变量_displayData,更新属性DisplayData

于 2013-09-23T06:50:53.357 回答