你提的問題是非常廣泛的,而且有些難以回答。簡而言之,您要查看的是控件上的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#語言不是很熟悉,因此我爲什麼掙扎了一點,工作是基於關掉一個,學習如我去基礎。 ^。^ –