C#WPF Listview项目刷新在OnPropertyChanged之后不触发

时间:2018-12-29 00:38:37

标签: c# wpf listview binding observablecollection

我创建了一个视图控件以图形方式显示数据。由于可能有许多视图实例,因此我使用了ListView控件,并将其绑定到可观察到的对象集合-图纸。 Sheets是SheetContainer对象的一个​​可观察的集合,其中包含工作表和名称。查看器显示工作表。

列表视图的xaml如下:

<ListView x:Name="ListViewSheetSlider" Height="170" Grid.Row="1" BorderThickness="0" 
                              ItemsSource="{Binding Sheets}" SelectionChanged="ListViewSheetSlider_SelectionChanged" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Name}" />
                <Viewbox Width="150" Height="150">
                    <SheetViewer:Viewer SetSheet="{Binding MySheet, NotifyOnSourceUpdated=True}" />
                </Viewbox>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

SelectionChanged方法如下:

private void UpdateSheetAndSlider()
{
    workspace.Sheets[SelectedSheetIndex] = MySheetDesigner.Sheet;
    //   ((SheetContainer)ListViewSheetSlider.SelectedItem).MySheet = MySheetDesigner.Sheet;
    Sheets[SelectedSheetIndex].MySheet = MySheetDesigner.Sheet;
}


private void MySheetDesigner_SheetChanged(object sender, EventArgs e)
{
    UpdateSheetAndSlider();
}

private void ListViewSheetSlider_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int index = ListViewSheetSlider.SelectedIndex;

    UpdateSheetAndSlider();
    SelectedSheetIndex = index;
    MySheetDesigner.Sheet = workspace.Sheets[index];
    ListViewSheetSlider.UpdateLayout();
}

SheetContainer实现很简单。看起来像:

public class SheetContainer : INotifyPropertyChanged
{
    public string Name { get; set; }

    private Sheet mySheet;
    public Sheet MySheet
    {
        get => mySheet;
        set
        {
            mySheet = value;

            OnPropertyChanged("MySheet");
        }
    }

    #region INotifyPropertyChanged Handler
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

}

查看器xaml和代码隐藏如下:

<UserControl x:Class="SheetViewer.Viewer"
             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:local="clr-namespace:SheetViewer"
             xmlns:cad="clr-namespace:Cad;assembly=Cad"
             mc:Ignorable="d"        
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <cad:Cad
            x:Name="CadSurface" >
        </cad:Cad>
    </Grid>
</UserControl>

后面的代码:

public partial class Viewer : UserControl
{

    public static readonly DependencyProperty SetSheetProperty =
             DependencyProperty.Register("SetSheet", typeof(Sheet), typeof(Viewer), new
                PropertyMetadata(default(Sheet), new PropertyChangedCallback(OnSetSheetChanged)));

    public Sheet SetSheet
    {
        get { return (Sheet)GetValue(SetSheetProperty); }
        set { SetValue(SetSheetProperty, value);}
    }

    private static void OnSetSheetChanged(DependencyObject d,
       DependencyPropertyChangedEventArgs e)
    {
        Viewer UserControl1Control = d as Viewer;
        UserControl1Control.OnSetSheetChanged(e);
    }

    private void OnSetSheetChanged(DependencyPropertyChangedEventArgs e)
    {
        //tbTest.Text = e.NewValue.ToString();
        Sheet s = (Sheet)e.NewValue;
        DrawSheet(s);
    }

    public Viewer()
    {
        InitializeComponent();
    }


    private void DrawSheet(Sheet sheet)
    {
        CadSurface.Draw();
    }

}

我希望 DrawSheet 方法在列表视图中的选择更改时触发。更改的属性的确在SheetContainer中触发,但不会进一步传播以绘制图纸。请注意,在填充列表视图时,将正确调用方法并绘制初始视图。

我已经花费了大量时间,但是不知何故缺少了关键要素。谁能帮忙吗?

谢谢

1 个答案:

答案 0 :(得分:1)

当分配给SetSheet的实例相同时,则不会触发DependencyPropertyChanged事件。这将解释一切。关键是DependencyProperty在设置值之前检查新旧值是否相等。如果它们相等,则不会传播任何更改。