【问题标题】:Check-box column does not change its state first time when row is not selected未选中行时,复选框列第一次不会更改其状态
【发布时间】:2015-02-16 10:00:11
【问题描述】:

这是我的 Employee 类。

public class Employee
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public bool Fire { get; set; }
    }

这就是 XAML 的设置方式。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Loaded="EmployeesGridLoaded">
        <DataGrid x:Name="gEmployees" HorizontalAlignment="Left" Margin="10,10,0,0" 
                  VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Fire" Binding="{Binding Fire}"  Width="1*" />
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="3*" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="3*" />
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

最后是EmployeesGridLoaded方法。

private void EmployeesGridLoaded(object sender, RoutedEventArgs e)
{
    List<Employee> Employees = new List<Employee>()
    {
        new Employee() { Fire = false, LastName = "Silly", FirstName = "Dude" },
        new Employee() { Fire = false, LastName = "Mean", FirstName = "Person" },
    };

        gEmployees.ItemsSource = Employees;
    }
}

问题是当我第一次单击 Fire 复选框时,它并没有立即将其状态更改为选中。我必须再单击一次才能将其状态更改为检查。它可能是第一次选择行。当我单击此单元格并且尚未在网格中选择行时,是否有第一次检查它?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    替换这行代码:

    <DataGridCheckBoxColumn Header="Fire" Binding="{Binding Fire}"  Width="1*" />
    

    与:

     <DataGridTemplateColumn Header="Fire" Width="1*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Path=Fire, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                  </DataGridTemplateColumn>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多