你提的问题是非常广泛的,而且有些难以回答。简而言之,您要查看的是控件上的Visibility
属性。
通过将Visibility
设置为Collapsed
,UI将不会显示该元素。如果需要,您可以根据另一个XAML元素的值或数据删除来设置Visibility
,但是您需要实现一个实现IValueConverter的类来进行转换。
其中一个最常见的值转换器是“布尔到可见性”转换器。如果你在互联网上搜索,你可以找到这些例子。您可以复制该方法并创建一个“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>
你用什么方法来设置你的应用程序中的值...你使用的是DataBinding吗?直接设置值?另外,你说XAML,但是什么类型:Silverlight,WIndows 8,WPF? – Robaticus
我正在使用WPF,至于我们如何设置值,我相信我们正在使用Databinding,我对分钟的C#语言不是很熟悉,因此我为什么挣扎了一点,工作是基于关掉一个,学习如我去基础。 ^。^ –