刷新属性上的ListView项已更改

时间:2016-08-11 10:10:52

标签: c# wpf listview

我有一个像这样的listView:

<ListView Grid.Row="1" x:Name="itemsListView" 
          BorderBrush="White" HorizontalAlignment="Stretch"
          ItemsSource="{Binding Path=Items}"
          SelectedItem="{Binding Path=ActualItem}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="{x:Static p:Resources.STATUS}">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <local:StatusElement State="{Binding Path=Status,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
                                                Height="20"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Header="{x:Static p:Resources.NAME}"
                            DisplayMemberBinding="{Binding Path=Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
        </GridView>
    </ListView.View>
</ListView>

它将名为Items的项目列表与许多字段绑定在一起。线程解析项目并在完成后更新字段。我更新字段时调用方法OnPropertyChanged。除了使用我的UserControl local:StatusElement的字段外,它适用于所有字段。我尝试像NAME一样显示我的状态,它正确刷新,但local:StatusElement没有刷新。永远不会达到StatusElement.State的get / set的断点。

我的userControl:

<UserControl ...
             x:Name="mainControl">
    <Grid Name="LabelGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Grid.Column="0" Name="MyImage"
               Source="{Binding Source, Source={StaticResource MyImage}}"
               Width="{Binding Height, ElementName=mainControl}"
               Height="{Binding Height, ElementName=mainControl}"/>
        <Label Grid.Column="1" Name="statusLabel"/>
    </Grid>
</UserControl>

public partial class StatusElement : UserControl
{

    // Dependency property backing variables
    public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State",
                 typeof(String), typeof(StatusElement), new UIPropertyMetadata(null));

    private string _state = "";
    public String State
    {
        get
        {
            return _state;
        }
        set
        {
            _state = value;
            RefreshState();
        }
    }

    private void RefreshState()
    {
        switch (State)
        {
            case "":
                MyImage.Visibility = Visibility.Hidden;
                break;
            default:
                MyImage.Visibility = Visibility.Visible;
                break;
        }
        statusLabel.Content = State;
    }

    public StatusElement()
    {
        InitializeComponent();
        RefreshState();
    }
}

为什么我的statusLabel的内容没有刷新?

2 个答案:

答案 0 :(得分:2)

您对State依赖项属性的定义是错误的。

它必须如下所示,其中CLR属性包装器必须调用拥有该属性的DependencyObject的GetValueSetValue方法。

public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
     "State",
     typeof(string),
     typeof(StatusElement),
     new PropertyMetadata(null, (o, e) => ((StatusElement)o).RefreshState()));

public string State
{
    get { return (string)GetValue(StateProperty); }
    set { SetValue(StateProperty, value); }
}

注意PropertyMetadata构造函数的第二个参数。它是一个静态PropertyChangedCallback,实现为lambda表达式。

答案 1 :(得分:1)

您的班级没有实施INotifyPropertyChanged活动。实现它以便更新发生。

  

通知客户端属性值已更改。

public partial class StatusElement : UserControl,INotifyPropertyChanged
{
 ....

public event PropertyChangedEventHandler PropertyChanged;

private void RefreshState([CallerMemberName]string prop = "")
{
    switch (State)
    {
        case "":
            MyImage.Visibility = Visibility.Hidden;
            break;
        default:
            MyImage.Visibility = Visibility.Visible;
            break;
    }
    statusLabel.Content = State;
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(prop));

    }
}
} 
相关问题