WPF CheckBox(IsThreeState ="True") [英] WPF CheckBox ( IsThreeState="True" )
问题描述
大家好,
我的应用程序中有一个CheckBox作为WPF Datagrid HeaderTemplate.我的应用程序基本上是一个电子邮件收件箱应用程序,因为我需要处理CheckBox的ThreeState.
1.如果已进行IsChecked,则需要在Datagrid中选择一些项(例如未读)
2.如果未选中,我需要在Datagrid中选择一些项目(例如Readed)
3.如果ThreeState我需要在Datagrid中选择一些项(例如Junk)
这是我的演示项目代码
Xaml代码
Hi all,
I have a CheckBox as a WPF Datagrid HeaderTemplate in my application. my application is basically a Email Inbox Application in that i need to handle ThreeState of CheckBox.
1. if IsChecked i need to select some items in the Datagrid (say UnReaded)
2. If UnChecked i need to select some items in the Datagrid (say Readed)
3. if ThreeState i need to select some items in the Datagrid (say Junk)
this is my Demo project code
Xaml Code
<Window x:Class="WPF_TEST.MainDisplayWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_TEST"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="WindowDisplay"
d:DesignHeight="414" d:DesignWidth="660" >
<Grid>
<DataGrid AutoGenerateColumns="False" Height="250" HorizontalAlignment="Left" Margin="93,58,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="469" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Content="Type" x:Name="chkHeader" IsChecked="{Binding ElementName=WindowDisplay, Path=IsHeaderSelected, Mode=TwoWay}" IsThreeState="True" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding ElementName=WindowDisplay, Path=IsHeaderSelected, Mode=TwoWay}" Content="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
背后的代码
Code Behind
namespace WPF_TEST
{
/// <summary>
/// Interaction logic for MainDisplayWindow.xaml
/// </summary>
public partial class MainDisplayWindow : Window
{
public bool? IsHeaderSelected
{
get { return (bool?)GetValue(IsHeaderSelectedProperty); }
set { SetValue(IsHeaderSelectedProperty, value); }
}
// Using a DependencyProperty as the backing store for IsHeaderSelected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsHeaderSelectedProperty =
DependencyProperty.Register("IsHeaderSelected", typeof(bool?), typeof(MainDisplayWindow), new UIPropertyMetadata(null));
public MainDisplayWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainDisplayWindow_Loaded);
}
void MainDisplayWindow_Loaded(object sender, RoutedEventArgs e)
{
MyClassData res = new MyClassData();
dataGrid1.ItemsSource = res;
}
}
public class MyClass : List<MyClass>
{
public string Name { get; set; }
public MyClass()
{
}
}
public class MyClassData : List<MyClass>
{
public MyClassData()
{
this.Add(new MyClass { Name = "Readed" });
this.Add(new MyClass { Name = "UnReaded" });
this.Add(new MyClass { Name = "Junk" });
}
}
}
任何人都有任何想法.
any one have any Idea.
推荐答案
我在stackoverflow中发现了此链接,可能对您有用.
链接
I found this link in stackoverflow which might be useful for you.
Link
这篇关于WPF CheckBox(IsThreeState ="True")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!