2015-12-07 168 views
1

我開始爲UWP開發。我正在嘗試創建一個ListView,其中填入x:bind。現在我想在所有單個項目上創建一個按鈕來刪除它們。類似的Windows 10郵件:刪除ListView項目上的按鈕

我已經創建了:

enter image description here

<ListView x:Name="listActivity" ItemsSource="{x:Bind list}" Grid.Column="0" Header="Activity List" HorizontalAlignment="Center" Width="auto" IsItemClickEnabled="True" ItemClick="showDel"> 
    <ListView.ItemTemplate> 
     <DataTemplate x:DataType="vm:Activity"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="3*"/> 
        <ColumnDefinition Width="1*"/> 
       </Grid.ColumnDefinitions> 
       <TextBlock x:Name="txt" Text="{x:Bind name}" Grid.Column="0"/> 
       <Button x:Name="delItem" Click="delActivity" Grid.Column="1" Visibility="Collapsed" Background="Transparent" Margin="100, 0, 0, 0"> 
        <SymbolIcon Symbol="Delete"/> 
       </Button> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

我想改變delItem按鈕visibility,但是從C#我不能用集合名稱調用它。我該怎麼做?

+0

要在ListView.ItemTemplate中獲取一個按鈕並在代碼中更改它的屬性,應該使用FrameworkTemplate.FindName方法。就像這個答案:http://stackoverflow.com/a/34118343/2946329 –

+0

我還建議你做什麼缺少'FindName'。您應該使用我在該答案中建議的FindDescendant。 –

+0

非常感謝你似乎工作得很好:D – user2625557

回答

1

我建議通過使用綁定來執行此操作。

一)一個新的屬性添加到名爲可見

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

    private bool _visible; 
    public bool Visible { 
     get { return _visible; } 
     set 
     { 
      _visible = value; 
      RaisePropertyChanged("Visible"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

B您的班級活動)也聲明瞭一類新布爾轉換爲可見

public class BooleanToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     return value is Visibility && (Visibility)value == Visibility.Visible; 
    } 
} 

C)和最後更新綁定。不要忘記單向模式設置。

<Page.Resources> 
    <local:BooleanToVisibilityConverter x:Key="VisibilityConverter"/> 
</Page.Resources> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <ListView ItemsSource="{x:Bind Activities}"> 
     <ListView.ItemTemplate> 
      <DataTemplate x:DataType="local:Activity"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="3*"/> 
         <ColumnDefinition Width="1*"/> 
        </Grid.ColumnDefinitions> 
        <TextBlock x:Name="txt" Text="{x:Bind Name}" Grid.Column="0"/> 
        <Button x:Name="delItem" Click="delItem_Click" Grid.Column="1" Visibility="{x:Bind Visible, Mode=OneWay, Converter={StaticResource VisibilityConverter}}" Background="Transparent" Margin="100, 0, 0, 0"> 
         <SymbolIcon Symbol="Delete"/> 
        </Button> 
       </Grid> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</Grid> 

您現在可以通過代碼直接訪問Visibility或控件。以下完整代碼供參考。

using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

namespace App1 
{ 
public sealed partial class MainPage : Page 
{ 
    public List<Activity> Activities { get; set; } 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     Activities = new List<Activity>(); 
     Activities.Add(new Activity { Name = "Activity1", Visible = true }); 
     Activities.Add(new Activity { Name = "Activity2", Visible = false }); 
    } 

    private void delItem_Click(object sender, RoutedEventArgs e) 
    { 
     Activities.First().Visible = false; 
    } 
} 

public class BooleanToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     return value is Visibility && (Visibility)value == Visibility.Visible; 
    } 
} 

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

    private bool _visible; 
    public bool Visible { 
     get { return _visible; } 
     set 
     { 
      _visible = value; 
      RaisePropertyChanged("Visible"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 
}