2017-05-31 177 views
1

如何更改wpf listview項目文本沒有listview1.Items[0].Refresh();方法?如何更改wpf listview項目文本

我的XAML代碼:

<ListView Height="233" HorizontalAlignment="Left" Margin="0,78,0,0" Name="listView1" VerticalAlignment="Top" Width="503" IsSynchronizedWithCurrentItem="True"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn x:Name="GridViewColumnName1" Header="Files" Width="120"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <Grid Width="{Binding ActualWidth, ElementName=GridViewColumnName1}"> 
           <StackPanel Orientation="Vertical" Grid.Column="1" Margin="8,0"> 
            <StackPanel Orientation="Horizontal"> 

             <Label Content="{Binding ItemsName,Mode=TwoWay}" Name="listnameLB"></Label> 

            </StackPanel> 
           </StackPanel> 
          </Grid> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
     </ListView.View> 
    </ListView> 

我的代碼:

private ListViewItemsData ListviewObject; 
public class ListViewItemsData 
{ 
    public string ItemsName { get; set; } 
} 

private void button2_Click(object sender, RoutedEventArgs e) 
{ 
    listView1.Items.Add(new ListViewItemsData() 
    { 
     ItemsName = "ABCD" 
    }); 
} 

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    //ListviewObject = new ListViewItemsData(); 
    ListviewObject = (ListViewItemsData)listView1.Items[0]; 
    ListviewObject.ItemsName = "EFGH"; 
    //listView1.Items.Refresh(); 
} 

我要改變 「ItemsName」 標籤內容爲Button1事件。

回答

0

ListViewItemsData類實現INotifyPropertyChanged和數據綁定ItemsName setter方法提高PropertyChanged事件:

public class ListViewItemsData : INotifyPropertyChanged 
{ 
    private string _itemsName; 
    public string ItemsName 
    { 
     get { return _itemsName; } 
     set { _itemsName = value; OnPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

(只),那麼這應該工作:

ListviewObject.ItemsName = "EFGH"; 
+0

Thaks!這項工作! :) – Gabee8