How to visually update checkboxes in UI that are bounded to boolean isChecked variable in WPF c#?

↘锁芯ラ 提交于 2019-12-12 01:14:02

问题


So I have a TreeView that has heirarchical data templates that have CheckBoxes in the lowest layer. These CheckBoxes are bound to a "isChecked" boolean value in the ViewModel. What I have been attempting to do is load the various variables indicating to me which checkboxes to check, change the boolean isChecked in the model to true, therefore resulting in the specific CheckBoxes in the TreeView being update visually. Here is my code for reference:

XAML: (of TreeView I am working with)

<DockPanel Name="test1" Margin="10,10,0,10" VerticalAlignment="Stretch" Grid.Row="3" Grid.RowSpan="7" Grid.Column="0">
        <DockPanel.Resources>
            <local:CheckBoxCommand x:Key="cbc"></local:CheckBoxCommand>
            <src:TreeViewFilter x:Key="MyList" />

            <HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}">
                <TextBlock Text="{Binding Path=NameAndCount}" FontSize="24"/>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}">
                <StackPanel Name="test" Orientation="Horizontal" VerticalAlignment="Center">
                    <CheckBox Command="{StaticResource cbc}"
                              CommandParameter="{Binding Path=NameAndParent}" Visibility="{Binding Path=CheckBoxVisible}" IsChecked="{Binding Path=isChecked, Mode=TwoWay}" VerticalAlignment="Center">
                    </CheckBox>
                    <TextBlock Text="{Binding Path=NameAndCount}" FontSize="16"/>
                    </StackPanel>
            </HierarchicalDataTemplate>

        </DockPanel.Resources>
        <TreeView Name="treeView1" BorderThickness="2" ItemsSource="{Binding Source={StaticResource MyList}, UpdateSourceTrigger=PropertyChanged}" TreeViewItem.Selected="filterByBatchStatus"/>
    </DockPanel>

C# code in ViewModel (pertaining to the problem):

public event PropertyChangedEventHandler PropertyChanged2;

protected void FirePropertyChanged2(string CheckBoxChecked)
    {
        if (PropertyChanged2 != null)
        {
            PropertyChanged2(this, new PropertyChangedEventArgs(CheckBoxChecked));
        }
    }

public static bool cbc;

public bool CheckBoxChecked
    {
        get { return (bool)GetValue(CheckBoxCheckedProperty); }
        set { SetValue(CheckBoxCheckedProperty, value); }
    }

public static readonly DependencyProperty CheckBoxCheckedProperty =
        DependencyProperty.Register("CheckBoxChecked", typeof(bool), typeof(OrderAttribute), new UIPropertyMetadata((bool)false));

bool _isChecked;
    public bool isChecked
    {
        get { return _isChecked; }

        set
        {
            if (_isChecked != value)
            {
                _isChecked = value;
                FirePropertyChanged2("CheckBoxChecked");
            }
        }
    }

If I load the values, update the booleans, and then expand the tree (So I can visually see them), the correct checkboxes are checked and everything works fine. The Problem comes into play when I load different checkboxes after initially visually seeing the first loaded checkboxes, change the booleans, then the Checkboxes do not change according, (dont change at all).

The part that stumps me is that if i do multiple loads, change the booleans over and over, WITHOUT expanding the tree and VISUALLY SEEING the boxes, and then expand the tree, the correct boxes are checked. As soon as I can visually expand and see the checkboxes, any post loading does not update the checkboxes, regardless if whether or not I expand or unexpand the tree.


回答1:


Wow... you have some serious issues here. First of all, as I said in your previous question, DependencyProperties don't belong into ViewModels. Second, your CheckBox is located inside a HierarchicalDataTemplate defined for the Type src:OrderAttribute, and therefore all bindings there are going to search for properties of that type. Third, it is not clear to me what string parameter you are invoking the PropertyChanged event with, but it should be the name of a CLR property in your class, to which the UI is bound. I strongly suggest you take a look at This WPF Tutorial to at least understand the basics of MVVM, INotifyPropertyChanged and DataTemplating, before trying to do complex hierarchical presentation layers.

Edit: Remove the DependencyProperty from the OrderAttribute Class, and use a regular bool property, then when calling PropertyChanged, do so by passing a string parameter containing the name of that property:

public bool MyBool 
{ 
   get { return _mybool; }
   set {
         _mybool = value;
         NotifyPropertyChanged("MyBool");
       }
}

public void NotifyPropertyChanged(string propertyName)
{
   if (PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
}


来源:https://stackoverflow.com/questions/13164045/how-to-visually-update-checkboxes-in-ui-that-are-bounded-to-boolean-ischecked-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!