【发布时间】:2022-01-13 08:20:22
【问题描述】:
我有一个转换器,它接受一个布尔值,并根据它是真还是假返回 A 或 B。转换器根据布尔值选择正确的值,但仅在开始时,如果我在运行时更改布尔值,转换器不会更新。
基本上,我有一个用户控件,其中有一个按钮,这个按钮切换“IsOpen”属性,这很有效。但是我有一个将 IsOpen 绑定到 Image(按钮)的 multibinder,它将根据 IsOpen 切换图像。但它没有更新,只保留开始时的值。 (IsOpen 确实会在点击时切换,这不是问题)
我在哪里进行多重绑定的用户控制:
<v:IconButton ColorPalette="{StaticResource MilkySolid}" ColorPaletteFore="{StaticResource BlackToBrightPalette}" IconMargin="0" Content="" VerticalAlignment="Top" Margin="0" HorizontalAlignment="Left" FontSize="1" Height="26" IconWidth="26" Click="IconButton_Click">
<v:IconButton.Image>
<MultiBinding Converter="{StaticResource AorBConverter}">
<Binding Path="IsOpen"/>
<Binding Source="{StaticResource collapseBTN}"/>
<Binding Source="{StaticResource expandBTN}"/>
</MultiBinding>
</v:IconButton.Image>
</v:IconButton>
CodeBehind(这部分有效)
private void IconButton_Click(object sender, RoutedEventArgs e)
{
IsOpen = !IsOpen;
}
public bool IsOpen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
public static readonly DependencyProperty IsOpenProperty =
DependencyProperty.Register("IsOpen", typeof(bool),
typeof(ParamNodeV), new PropertyMetadata(false));
用户控件的视图模型(这也有效)
public bool IsOpen
{
get { return isOpen; }
set
{
isOpen = value;
OnPropertyChanged(nameof(IsOpen));
}
}
所以,就像我说的,转换器根据布尔值选择正确的图像。但如果我在运行时更新布尔值,它不会更新。
如果你问我为什么我不只是使用触发器:我正在尝试从我的 UserControl (ParamNodeV) 更改 CustomControl (IconButton) 上的图像,但我不知道如何访问属性来自 ParamNodeV 的 IconButton,没有完全覆盖样式/模板。因此,如果有人帮助我使用转换器或帮助我如何从 UserControl 导航到 IconButton 的 Image 属性,而无需覆盖样式/模板
【问题讨论】:
-
如果你没有两个同名的属性,这个问题会更清楚。为什么有一个“用户控件的视图模型”?如果控件的 IsOpen 属性绑定到视图模型的 IsOpen 属性,请确保 Binding 是 TwoWay。
标签: wpf data-binding user-controls multibinding