【发布时间】:2018-07-18 03:37:55
【问题描述】:
所以我在我的 xaml 中设置了一个 ItemsControl:
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<local:ToggleButton Command="{Binding DataContext.ItemSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type WrapPanel}}}"
CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}"
Text="{Binding DataContext.ItemEnum, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Converter={StaticResource EnumToStringConverter}}"
IsActive="{Binding DataContext.Selected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, UpdateSourceTrigger=PropertyChanged}"
Width="96"
Height="88"
Margin="5" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我有 4 个依赖属性 Command、CommandParameter、Text 和 IsActive。
前 3 个依赖属性正常工作,设置了文本,命令回调与参数一起工作。
但IsActive 属性不起作用。
主视图模型中的 Items 属性定义为:
List<ItemViewModel> Items { get; set; }
ItemViewModel 定义为:
public class ItemViewModel : INotifyPropertyChanged
{
public ItemViewModel()
{
this.Selected = true;
}
private bool? _selected;
public event PropertyChangedEventHandler PropertyChanged;
public ItemEnum ItemEnum { get; set; }
public bool? Selected
{
get { return this._selected; }
set
{
this._selected = value;
this.OnPropertyChanged(nameof(this.Selected));
}
}
protected virtual void OnPropertyChanged(string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
ToggleButton.xaml.cs 文件中 IsActive 属性的依赖属性如下所示:
public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(nameof(IsActive), typeof(bool?), typeof(ToggleButton), new PropertyMetadata(null, IsActiveSetCallback));
private static void IsActiveSetCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var button = (ToggleButton)obj;
button.IsActive = (bool)(e.NewValue ?? e.OldValue);0xa5, 0xa5));
}
我在主视图模型中的命令回调如下所示:
this.ItemSelectedCommand =
new DelegateCommand(
itemVm =>
{
bool? setTo = !((ItemViewModel) itemVm).Selected;
this.Items.ForEach(i => i.Selected = false);
((ItemViewModel) itemVm).Selected = setTo;
});
同样,其他依赖属性(与IsActiveProperty 定义基本相同)正常工作,因此当我单击该项目时,将调用上述命令(通过断点验证),该项目的 Selected 标志被正确切换,但 IsActiveSetCallback永远不会被击中。我看不出我做错了什么,但很明显是有问题。
有人看到我看不到的东西吗?
提前致谢!
【问题讨论】: