2016-08-11 63 views
0

我有這樣一個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 ... 
      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> 

和:

​​

爲什麼我statusLabel的內容不會刷新?

+0

請注意,UpdateSourceTrigger = PropertyChanged在單向綁定中沒有效果(因爲沒有從目標到源的數據流)。除此之外,顯式設置'Mode = OneWay'通常是多餘的,因爲這已經是大多數依賴屬性的默認值。 – Clemens

回答

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表達式實現。

+0

完美!我不知道這種方式,謝謝。 –

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)); 

    } 
} 
} 
+0

通常不需要在UserControl中實現INotifyPropertyChanged,因爲屬性通常定義爲依賴項屬性,該屬性已經具有內置的更改通知機制。 – Clemens

+0

有沒有我可以查看的教程? – Sadique

+0

Google「wpf自定義依賴項屬性」。 – Clemens