你的问题非常广泛,有点难以回答。简而言之,您要查看的是Visibility
控件上的属性。
通过设置Visibility
为Collapsed
,UI 将不显示该元素。如果需要,您可以Visibility
根据另一个 XAML 元素或数据元素的值进行设置,但您需要实现一个实现 IValueConverter 的类来进行转换。
最常见的值转换器之一是“布尔到可见性”转换器。如果您在 Internet 上搜索,您将能够找到这些示例。您可以复制该方法并创建“EmptyToVisibilityConverter”或“NullToVisibilityConverter”或您需要的任何其他内容。拥有该转换器后,您只需在绑定中指定它以获取可见性。例如:
<Page.Resources>
<conv:NullToVisibilityConverter x:Key="NullToVis"/>
</Page.Resources>
<CheckBox ... Checked={Binding ThisBoxIsChecked}
Visibility={Binding SomeOtherValue,
Converter={StaticResource NullToVis}}"/>
请记住,控件内容和可见性属性之间的数据绑定不必相同。您可以将您的内容绑定到一个值,并将可见性绑定到另一个值。
如果您不使用数据绑定,则必须在代码隐藏中设置这些。但是你为什么不使用数据绑定呢?
编辑:这是一个工作示例。
如果这不是您要找的东西,那么我很迟钝并且不理解这个问题
我建议您开始一个空白项目,将其放入其中,然后尝试一下,以了解如何进行设置。XAML 的学习曲线相对陡峭,通常有几种方法可以完成您需要的工作,但您确实需要对数据绑定和 INotifyPropertyChanged(我在本示例中没有涉及)有基本的了解。
这是 C# 代码:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace CheckboxList
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Create a viewmodel and add some data to it.
var viewModel = new MyViewModel();
viewModel.Items.Add(new Data() {Name = "lol", Type = "lol", Selected = true});
viewModel.Items.Add(new Data() { Name = "lol", Type = "not_lol", Selected = true });
viewModel.Items.Add(new Data() { Name = "not_lol", Type = "not_lol", Selected = true });
//Set the window's datacontext to the ViewModel. This will make binding work.
this.DataContext = viewModel;
}
}
//This is the ViewModel used to bind your data
public class MyViewModel
{
//This could just be a List<Data> but ObservableCollection<T> will automatically
//update your UI when items are added or removed from the collection.
public ObservableCollection<Data> Items { get; set; }
public MyViewModel()
{
Items = new ObservableCollection<Data>();
}
}
//Just a sample class to hold the data for the grid.
//This is the class that is contained in the ObservableColleciton in the ViewModel
public class Data
{
public string Name { get; set; }
public string Type { get; set; }
public bool Selected { get; set; }
}
//This is an example converter. It looks to see if the element is set to "lol"
//If so, it returns Visibility.Collapsed. Otherwise, it returns Visibility.Visible.
public class LolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is string)
{
var input = (string) value;
if (string.Equals(input, "lol", StringComparison.CurrentCultureIgnoreCase))
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
和 XAML:
<Window x:Class="CheckboxList.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:CheckboxList="clr-namespace:CheckboxList"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- Create an instance of LolToVisibilityConverter and call it "LolToVis" -->
<CheckboxList:LolToVisibilityConverter x:Key="LolToVis"/>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Items}"> <!--Bind the contents of the Items collection in our viewmodel -->
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"/> <!-- bind this element to this column-->
<GridViewColumn Width="140" Header="Type" DisplayMemberBinding="{Binding Type}"/> <!-- bind this element to this column-->
<GridViewColumn Width="140" Header="Selected" > <!-- because we don't want this to just display true/false, we need to set up a template-->
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- we set the Visibility property to Name, and the converter to LolToVis-->
<!-- whenever this field will be displayed, it calls the converter to convert the string to a Visibility value-->
<!-- The visibility value is checked to determine whether or not the element should be displayed-->
<CheckBox IsChecked="{Binding Selected}" Visibility="{Binding Name, Converter={StaticResource LolToVis}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>