带复选框的WPF工具箱数据网格:如何实现和接收值
本文关键字:实现 何实现 WPF 复选框 工具箱 数据 网格 数据网 | 更新日期: 2023-09-27 17:52:40
我的datagrid不打算从数据库中读取任何值,但只需要读取文本列表作为选项名称。如何在WPF中实现它?我的意思是,我不知道这个的xaml和代码来读取最终用户的选项回复。这将是确切的网格视图:
------------------
checkbox | Black
------------------
checkbox | White
------------------
checkbox | Blue
------------------
checkbox | Green
------------------
checkbox | Grey
------------------
checkbox | Orange
------------------
如果用户选择并单击提交按钮,我需要以某种方式读取用户选中的值。我可以知道我如何实现这个或者有更好的方法来实现这个函数吗?谢谢您的回复。
编辑后的文件:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:ColorBox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="ColorBox.backoption"
x:Name="Window"
Title="backoption"
Width="977" Height="637" Background="#FF333333" xmlns:WPFtoolkit="http://schemas.microsoft.com/wpf/2008/toolkit">
<DataTemplate DataType="{x:Type my:MyType}">
<Grid x:Name="LayoutRoot">
<Grid Margin="43,101,42,48">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Checked="{Binding Path=Checked}"/>
<TextBox Text="{Binding Path=Permission}"/>
</Grid>
</Grid>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
</DataTemplate>
</Window>
和WPF文件后面的代码:
namespace ColorBox
{
public partial class backoption: Window
{
public backoption()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
MyType Beta = new MyType();
Beta.Checked = false;
Beta.Permission = "Hello";
}
}
public class MyType
{
public bool Checked { get; set; }
public string Permission { get; set; }
}
}
您可以使用DataTemplates将列表与数据(颜色)绑定。您的数据模板将是:
<DataTemplate DataType="{x:Type my:MyType}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding Path=Selected}"/>
<TextBlock Grid.Column="1" Text="{Binding Path=ColorName}"/>
</Grid>
</DataTemplate>
你的班级将是:
public class MyType // Should implement INotifyPropertyChanged.
{
public bool Selected {get; set;}
public string ColorName {get;set;}
//....
}
和所有剩下的是添加你的"MyType"的列表到ViewModel和绑定它作为ItemsSource到你的项目容器