所以我有一個TreeView,它具有在最底層有複選框的層次數據模板。這些CheckBox綁定到ViewModel中的「isChecked」布爾值。我一直試圖做的是加載各種變量,向我指示要檢查哪些複選框,將模型中的布爾值IsChecked更改爲true,從而導致TreeView中的特定CheckBox被更新。這裏是我的參考代碼:如何可視地更新UI中綁定到WPF c#中的boolean isChecked變量的複選框?
XAML:在視圖模型(樹視圖我有工作)
<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#代碼(關於這個問題):
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");
}
}
}
如果我加載值,更新布爾值,然後展開樹(所以我可以直觀地看到它們),檢查正確的複選框,一切正常。當最初直觀地看到第一個加載的複選框後,加載不同的複選框,改變布爾值,然後複選框不會根據改變(根本不改變)。
使我困惑的部分是,如果我做了多個負載,一遍又一遍地更改布爾值,不展開樹並且可視化地看到這些框,然後展開樹,檢查正確的框。只要我可以直觀地展開並查看複選框,則無論是否展開或取消展開樹,任何後期加載都不會更新複選框。
它看起來像你'只對「CheckBoxChecked」執行「FirePropertyChanged2」,而不是「isChecked」,你將綁定 – Pyritie