我建議通過使用綁定來執行此操作。
一)一個新的屬性添加到名爲可見
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));
}
}
}
}
要在ListView.ItemTemplate中獲取一個按鈕並在代碼中更改它的屬性,應該使用FrameworkTemplate.FindName方法。就像這個答案:http://stackoverflow.com/a/34118343/2946329 –
我還建議你做什麼缺少'FindName'。您應該使用我在該答案中建議的FindDescendant。 –
非常感謝你似乎工作得很好:D – user2625557