WPF - Datagrid中的复选框,用于设置行的背景颜色

时间:2013-01-08 18:26:33

标签: wpf datagrid checkbox

我正在尝试根据选中的两个复选框中的哪一个来更改网格的背景颜色。一旦我在网格中选择了行,我想使用数据插入到不同的表中,其中一个字段的值取决于在网格中选择了哪两个复选框。以下是我目前的代码。我尝试了几件没有成功的事情。

XAML:

<Window x:Class="TESTWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="43,35,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1" RenderTransformOrigin="0.507,0.545"/>
    <DataGrid Name="dg"
              HorizontalAlignment="Left" 
              Margin="10,81,0,0" 
              VerticalAlignment="Top" 
              Height="214"                   
              AutoGenerateColumns="False"
              ScrollViewer.VerticalScrollBarVisibility="Auto" Width="497">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="OK" Width="40">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="OKCCheckBox" 
                                  HorizontalAlignment="Center" 
                                  IsChecked="{Binding Path=NONC, UpdateSourceTrigger=PropertyChanged}"
                                  />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
                            <DataGridTextColumn Header="RMA ID"
                                Width="80"
                                Binding="{Binding Path=RMA_ID}"
                                />
            <DataGridTextColumn Header="SERIAL #"
                                Width="80"
                                Binding="{Binding Path=SERIAL_ID}"
                                />
            <DataGridTextColumn Header="PART #"
                                Width="150"
                                Binding="{Binding Path=INV_ITEM_ID_ORDERED}"
                                />
            <DataGridTextColumn Header="QTY"
                                Width="50"
                                Binding="{Binding Path=QTY}"
                                />
            <DataGridTemplateColumn Header="NonC" Width="40">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="NONCCheckBox" 
                                  HorizontalAlignment="Center" 
                                  IsChecked="{Binding Path=NONC, UpdateSourceTrigger=PropertyChanged}"
                                  />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="Beige"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="Bisque"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="Bisque"/>
                    </Trigger>
                    <DataTrigger Binding="{Binding Path=RMA_ID}" Value="HJRA0000P4">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
</Grid>

C#代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using Oracle.DataAccess.Client;

namespace TESTWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public DataSet GetItemInformationRMAID()
    {
        string _connstring = "Data Source=ORCL;User Id=TEST;Password=TEST;";
        string _sql;
        DataSet _ds = new DataSet();
        try
        {
            OracleConnection _connObj = new OracleConnection(_connstring);
            _connObj.Open();
            _sql = "select rma_id, serial_id, inv_item_id_ordered, qty from return_line";
            OracleDataAdapter _adapterObj = new OracleDataAdapter();
            OracleCommand cmd = new OracleCommand(_sql, _connObj);
            _adapterObj.SelectCommand = cmd;
            _adapterObj.Fill(_ds);
            _connObj.Close();
            _connObj.Dispose();
            _connObj = null;
            return _ds;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            return _ds;
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        dg.ItemsSource = GetItemInformationRMAID().Tables[0].DefaultView;
    }
}
}

我确定我错过了一些明显的东西,但作为一个新手,我只是没有看到它。任何帮助表示赞赏。

谢谢,

马格努斯

1 个答案:

答案 0 :(得分:0)

为什么两个复选框都绑定到一个属性Binding Path = NONC?