0
votes

I am working on a wpf application and I am dealing with checkboxes now The problem when I set the Ischecked property to "True" like this:

   <CheckBox  Visibility="{Binding checkVisibility}" IsChecked="true" 
   IsEnabled="True"></CheckBox>

I get my checkbox checked But when I try to bind a booléan property that it's value is "true" i get my checkbox always unchecked I can't see where is the proplem

this is my xaml

 <CheckBox  Visibility="{Binding checkVisibility}" IsChecked="{Binding Path=test,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" IsEnabled="True"></CheckBox>

this is my property

public bool _test = true;

public bool test {
    get {
        return _test;
    }
    set {
        if (_test == value) {
            return;
        }

        _test = value;
        RaisePropertyChanged("test");
    }
}

I want my checkbox to reflect my property value and vice versa but it's not the case as my checkbox is always unchecked I am missing something?

Edit Here is my VM:

namespace X{
public class MyViewModel :  
{ 
public MyViewModel()
{
  TestCheckbox();
}
#Region Properties

    public bool _test1 = true;
    public bool test1
    {
        get
        {
            return _test1;
        }

        set
        {
            if (_test1 == value)
            {
                return;
            }

            _test1 = value;
            RaisePropertyChanged("test1");
        }
    }

 ObservableCollection<Output> _output_List;
    public ObservableCollection<Output> output_List
    {
        get { return _output_List; }
        set
        {
            if (_output_List == value) return;
            _output_List = value;
            RaisePropertyChanged("output_List");
        }

    }
#EndRegion
#Region ButtonCommand
  private RelayCommand _SetOutputPropertiesCommand;

    public RelayCommand SetOutputPropertiesCommand => _SetOutputPropertiesCommand
                ?? (_SetOutputPropertiesCommand = new RelayCommand(
                () =>
                {   
                        foreach (Output item in output_List)
                        {
                          item.myvalue=Test2;
                        }
                    }

                }));

#EndRegion
#Region Method
  public void TestCheckbox()
    {
     Console.Writeline(Test2);
    }
#EndRegion    

            }
  public class Output
{
    public string label { get; set; }
    public string type { get; set; }
    public bool myvalue { get; set; }
    public bool Test2 { get; set; }
    [System.ComponentModel.DataAnnotations.Schema.NotMapped]
    [JsonIgnore]
    public string checkVisibility { get; set; }
}
}

My Xaml : my checkboxes are integrated in a DataGrid view

                    <DataGrid x:Name ="GridO"  Style="{x:Null}"
                    ItemsSource= "{Binding output_List,UpdateSourceTrigger=PropertyChanged}"

                  AutoGenerateColumns="False" CellStyle="{StaticResource Body_Content_DataGrid_Centering}"
                  Margin="5,0" IsReadOnly="True" SelectionMode="Single" RowHeight="50" Height="Auto">


                        <DataGrid.Columns>

                            <DataGridTextColumn Width="40*" Binding="{Binding label}">
                                <DataGridTextColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock  Text="Input"></TextBlock>
                                    </DataTemplate>
                                </DataGridTextColumn.HeaderTemplate>
                            </DataGridTextColumn>

                            <DataGridTextColumn  Width="40*" Binding="{Binding type}">
                                <DataGridTextColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock  Text = "Type"></TextBlock>
                                    </DataTemplate>
                                </DataGridTextColumn.HeaderTemplate>
                            </DataGridTextColumn>


                            <DataGridTemplateColumn Width="20*" >
                                <DataGridTemplateColumn.Header>
                                    <TextBlock Text="Value" />
                                </DataGridTemplateColumn.Header>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                            <CheckBox Margin="20,0,0,0" Visibility="{Binding checkVisibility }" IsChecked="{Binding Test2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsEnabled="True"></CheckBox>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                        </DataGrid.Columns>
                    </DataGrid>
   <Button x:Name="Valid_output" Cursor="Hand" Background="Transparent"  Height="55"  Width="140" Margin="0,0,20,0" Command="{Binding SetOutputPropertiesCommand}"  >

The bind is Working with "Test2" and not working with "Test1" which not in the same class as Test2 NB: the Button command(which is in the same class as "Test1") is working well

My DataTemplate: are in the App.xaml

  xmlns:v="clr-namespace:X.View"
  xmlns:vm="clr-namespace:X"
   <DataTemplate DataType="{x:Type vm:MyViewModel}">
            <v:MyWindow/>
2
Are you certain of your DataContext? Binding setup looks correct, so I assume your checkbox simply cannot find your test property to bind with.DonBoitnott
Correct on binding. Your overall form DATACONTEXT should be bound to your class that contains the public get/set "Test" property being exposed, then it should work.DRapp
@DRapp My DataContext is fine as I am binding other elements in my interface (labels...) and it's working the only issue is with Checkboxsouiher najah
Is the checkVisibility property in the same class as the test? Is the visibility binding working?Artholl
@Artholl no they are not in the same class checkvisibility is working well I moved my test prperty to the same class and it worked!!souiher najah

2 Answers

0
votes

I see the main problem in the DataGrid. You set ItemsSource to a collection, so for every row in the DataGrid the data source is one item in that collection. Not your MyViewModel.

This is the reason, why the Test2 is working - it is in the class which instances are in the collection.

You could try to add CheckBox to the window without DataGrid and bind the Test1 - it should work.

If you really want to use some property from your MyViewModel, you can, but it is not that easy. You have to have there something like:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace"

<!--...something...-->

<DataGrid.Resources>
   <local:BindingProxy x:Key="Proxy" Data="{Binding}" />
</DataGrid.Resources>

<!--...something...-->

And then your binding in the DataGrid will look like this:

IsChecked="{Binding Data.Test1, Source={StaticResource Proxy}}"

Of course I don't know the exact settings of your application so you will have to complete/change the code according to them.

-2
votes

I suggest you to add a ViewModelBase class, like that

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = 
    null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Extends your ViewModel with this ViewModelBase class and implements your property

private bool myProperty;

public bool MyProperty { get; set; OnPropertyChanged(); }

Then you just have to bind on your property

<CheckBox IsChecked="{Binding MyProperty}"></CheckBox>

EDIT: To set your ViewModel as DataContext for your View you can set it from the code

MyView.DataContext = new MyViewModel();

or from the view, in the Window/User Control

<Window x:Class="MyApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApplication"
    Title="MainWindow" Height="350" Width="525"
    DataContext="local.MyViewModel">