简体   繁体   English

WPF Toolkit Datagrid带有复选框:如何实现和接收值?

[英]WPF Toolkit Datagrid with checkbox: How to implement and receive the value?

My datagrid is not going to read any value from database, but just need to read a list of text to be the option name. 我的datagrid不会从数据库中读取任何值,而只需要读取文本列表作为选项名称即可。 how to implement it in wpf ? 如何在WPF中实现它? i mean i don't know what the xaml for this and code to read the option reply back by the end user. 我的意思是我不知道这个xaml是什么,并且代码读取该选项会被最终用户回复。 this will be the exact grid view: 这将是确切的网格视图:

------------------
checkbox | Black
------------------
checkbox | White
------------------
checkbox | Blue
------------------
checkbox | Green
------------------
checkbox | Grey
------------------
checkbox | Orange
------------------  

if the user selected and clicked on submit button, i need someway to read user checked value. 如果用户选择并单击了提交按钮,则我需要以某种方式读取用户检查的值。 may i know how could i implement this or is there any better way for this function ? 我可以知道如何实现此功能,或者是否有更好的方法来实现此功能? Thank you for reply. 感谢您的回复。

The file after edit with solution: 用解决方案编辑后的文件:

<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>

and the code behind the wpf file : 以及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; }
    }
}

You can bind a list with your data (colors) using DataTemplates. 您可以使用DataTemplates将列表与数据(颜色)绑定。 Your data template will be: 您的数据模板将是:

<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>

Your class will be: 您的课程将是:

public class MyType // Should implement INotifyPropertyChanged.
{
    public bool Selected {get; set;}
    public string ColorName {get;set;}

    //....
}

And all the rest is to add your "MyType"s list to ViewModel and bind it as ItemsSource to your items container. 剩下的就是将“ MyType”列表添加到ViewModel并将其作为ItemsSource绑定到您的项容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM