WPF数据模板和属性中的奇怪行为。
我有一个DataTemplate,它在StackPanel中显示按钮。每当用户单击按钮时,按钮都会发光。因此,我在模板中编写了必要的DataTrigger,并在绑定的属性中编写了布尔条件。详情如下:WPF数据模板和属性中的奇怪行为。,wpf,data-binding,datatemplate,Wpf,Data Binding,Datatemplate,我有一个DataTemplate,它在StackPanel中显示按钮。每当用户单击按钮时,按钮都会发光。因此,我在模板中编写了必要的DataTrigger,并在绑定的属性中编写了布尔条件。详情如下: <DataTemplate x:Key="ActionItemsTemplate" DataType="ActionItemViewModel"> <ItemsControl IsTabStop="False" ItemsSource="{Binding}" Ma
<DataTemplate x:Key="ActionItemsTemplate" DataType="ActionItemViewModel">
<ItemsControl IsTabStop="False" ItemsSource="{Binding}" Margin="6,2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="ActionButton" Command="{Binding Path=Command }" Content="{Binding Path=DisplayName}" Style="{DynamicResource HeaderButton}"/>
<!-- Set special values for Selected Item -->
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter TargetName="ActionButton" Property="Style" Value="{DynamicResource MainWindowSelectedButton}"/>
<!--Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}"-->
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
有什么问题吗
编辑:我发现在这种情况下,问题可能在于数据绑定
现在我遇到了一个类似的问题,我将Expander控件的IsExpanded属性绑定到TabPanel中的bool属性。当我切换bool属性时,值会在后面更改,但不会反映在显示中。然而,假设我更改选项卡并返回,我看到更改已经发生。这与这个问题有关吗
我再一次想知道问题是什么,可以把它缩小一点:
编辑2:第二个问题的解决方案:我发现每当对INotifyPropertyChanged接口进行编程更新时,都需要调用INotifyPropertyChanged接口的OnPropertyChanged
IsExpanded属性已更新。至于最初的问题,情况似乎并非如此,我仍在努力找出问题所在 我被引导相信你不能取代这个收藏,只能改变它的内容 我正在绑定的属性中的布尔条件
我不完全确定您在这里的意思,但我猜测这是您命令上的IsSelected属性,并且您忘记为该属性引发PropertyChanged。是的,在更改其内容,然后引发INotifyPropertyChanged事件处理程序后,将调用该属性的get方法,然后返回期望值,但我不明白,为什么我需要再次重新分配变量。。。。。。。。。。。
void Test1()
{
_commands[0].IsSelected = !_commands[0].IsSelected;
_commands[0] = _commands[0]; // Does not work If this line is commented out
ActionItems = _commands;
}