Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Updating IsChekced in Style Setter not working

$
0
0

Hi All,

I am trying to update one check box value based on another one and trying to do it from the style. Below is my code and its not working. Looking forward for help.

Xaml

<Window x:Class="CheckBoxBinding.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><CheckBox Name="CheckBox1"
                  Margin="10"
                  Height="Auto"
                  Width="Auto"
                  Content="Box1"
                  IsChecked="{Binding Box1Checked}"/><CheckBox Name="CheckBox2"
                  Margin="30"
                  Height="Auto"
                  Width="Auto"
                  Content="Box2"
                  IsChecked="{Binding Box2Checked}"><CheckBox.Style><Style TargetType="CheckBox"><Style.Triggers><DataTrigger Binding="{Binding IsChecked, ElementName=CheckBox1}"
                                     Value="True"><Setter Property="IsChecked"
                                    Value="True" /></DataTrigger></Style.Triggers></Style></CheckBox.Style></CheckBox></Grid></Window>


ViewModel


namespace CheckBoxBinding
{
    using System.ComponentModel;

    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            this.Box1Checked = false;
            this.Box2Checked = false;
        }

        private bool box1Checked;

        public bool Box1Checked
        {
            get
            {
                return this.box1Checked;
            }

            set
            {
                this.box1Checked = value;
                this.OnPropertyChanged("Box1Checked");
            }
        }

        private bool box2Checked;

        public bool Box2Checked
        {
            get
            {
                return this.box2Checked;
            }

            set
            {
                this.box2Checked = value;
                this.OnPropertyChanged("Box2Checked");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

App.Xaml.cs

namespace CheckBoxBinding
{
    using System.Windows;

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow window = new MainWindow();
            ViewModel viewmodel = new ViewModel();
            window.DataContext = viewmodel;
            window.Show();
        }
    }
}


Thanks



Ramakrishna



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>