jeudi 23 juillet 2015

WPF DataTrigger Binding doesn´t work

i want in my application a function like if the first checkbox in a row is checked, the second in the same row has to be enabled. The other case it has to happen is, that if the first checkbox is unchecked, the second has to be disabled. Why does my binding on the first checkbox doesen´t work?

XAML:

 <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://ift.tt/o66D3f"
            xmlns:x="http://ift.tt/mPTqtT"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel LastChildFill="True">
            <Button DockPanel.Dock="Bottom" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
            <DataGrid x:Name="dgGrid" ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Top" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Text" Binding="{Binding Path=Text}" />
                      <DataGridTemplateColumn Header="Var.1">
                       <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <CheckBox x:Name="cb1" IsEnabled="True">
                            </CheckBox>
                        </DataTemplate>
                      </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
             <DataGridTemplateColumn Header="Var2">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox x:Name="cb2">
                        <CheckBox.Style>
                            <Style TargetType="{x:Type CheckBox}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=cb1}" Value="True">
                                        <Setter Property="IsEnabled" Value="True" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=cb1}" Value="False">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </CheckBox.Style>
                    </CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
            </DataGrid>
        </DockPanel>

Code:

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;

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

            List<GridItem> data = new List<GridItem>();


            for (int i = 0; i < 5; i++)
            {
                data.Add(new GridItem()
                    {
                        Text = "Item " + i
                    });

                data.Add(new GridItem()
                    {
                        Text = "ItemX " + i
                    });

            }


            this.dgGrid.ItemsSource = data;
        }

  class GridItem
    {
        public string Text
        {
            get;
            set;
        }
    }




Aucun commentaire:

Enregistrer un commentaire