【问题标题】:datagrid check box column clicked单击数据网格复选框列
【发布时间】:2015-10-06 13:26:24
【问题描述】:

我有一个数据网格,它从项目源动态获取数据。

项目来源的一个字段是bool(复选框列),我想知道用户何时单击复选框。

这是什么活动?

xml 代码:

<Window x:Class="ProtocolAnalyzer.createByProtocol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="createByProtocol" Height="506" Width="384">

    <Grid Margin="0,0,2,4">
       <DataGrid x:Name="dataGridTable" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="452" Width="245" SelectedCellsChanged="dataGridTable_SelectedCellsChanged" >
                  <DataGrid.Columns>           
                 </DataGrid.Columns>
       </DataGrid>
       <Button Content="Create" HorizontalAlignment="Left" Margin="275,165,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="Button_Click"/>

     </Grid>
</Window>

C#:

public class TableList
{
    private string _field;

    public string Field
    {
        get { return _field; }
        set { _field = value; }
    }
    private string _val;

    public string Val
    {
        get { return _val; }
        set { _val = value; }
    }

    private bool _calc;

    public bool Calc
    {
        get { return _calc; }
        set { _calc = value; }
    }

}


/// <summary>
/// Interaction logic for createByProtocol.xaml
/// </summary>
public partial class createByProtocol : Window
{
    private ProtocolData.ProtocolData.Protocols _runningProtocol;
    private List<TableList> _Ltbl;
 public createByProtocol(ProtocolData.ProtocolData.Protocols protocol)
    {
        InitializeComponent();
        _runningProtocol = protocol;
        _Ltbl = new List<TableList>();

        buildTable();
    }


    private void buildTable()
    {
        switch (_runningProtocol)
        {
            case  ProtocolData.ProtocolData.Protocols.ZBM:
                TableList tl = new TableList();
                tl.Field = "Length";
                tl.Val = "";
                tl.Calc = false;
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "OpCode";
                tl.Val = "";                    
                _Ltbl.Add(tl);


                tl = new TableList();
                tl.Field = "REQ_ID";
                tl.Val = "";                    
                _Ltbl.Add(tl);


                tl = new TableList();
                tl.Field = "Message Type";
                tl.Val = "";                    
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "DATA";
                tl.Val = "";                    
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "CS";
                tl.Val = "";                    
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "EOM";
                tl.Val = "0A";                    
                _Ltbl.Add(tl);

                break;
        }

        dataGridTable.ItemsSource = _Ltbl;
    }
 }

【问题讨论】:

  • 首先,使用自动属性
  • 自动属性是什么意思?
  • 真实姓名是Auto-Implemented Properties,它允许你写public string Val{get;set;}而不是private string _val; public string Val { get { return _val; } set { _val = value; } }
  • 好的。但这如何回答我的问题?
  • 不是,这就是为什么它是评论而不是答案:)。我会看一下Click="Button_Click",这意味着在单击按钮时,将调用函数Button_Click

标签: c# wpf datagrid itemssource


【解决方案1】:

看起来您正在使用自动生成的列或 DataGridCheckBoxColumn,因此想知道如何检查 CheckBox 是否已选中/未选中。您必须在 DataGrid 级别处理 CheckBox.Click、CheckBox.Checked、CheckBox.Unchecked 附加事件。

<DataGrid  x:Name="DgrdRight" CheckBox.Click="DgrdRight_Click"  CheckBox.Checked="DgrdRight_Checked" CheckBox.Unchecked="DgrdRight_Unchecked"  Grid.Column="1" Grid.Row="0" Margin="10,0,0,0" ColumnWidth="SizeToHeader" AutoGenerateColumns="False">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
      <DataGridCheckBoxColumn ... />
   </DataGrid.Columns>
</DataGrid>

代码中的处理程序:

        private void DgrdRight_Checked(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Checked");
        }

        private void DgrdRight_Unchecked(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("UnChecked");
        }

        private void DgrdRight_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Clicked");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多