我有以下 DependencyProperty
:
public bool IterationSelected
{
get { return (bool)GetValue(IterationSelectedProperty); }
set { SetValue(IterationSelectedProperty, value); }
}
public static readonly DependencyProperty IterationSelectedProperty =
DependencyProperty.Register("IterationSelected", typeof(bool),
typeof(MainMediator), new UIPropertyMetadata(false));
(这是从 VS 模板生成的)
然后我有这个 DataTrigger
来更新它:
<Grid.Style>
<Style>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IsOpenFalseAndIsCheckedTrueMultiConverter}">
<Binding ElementName="popSelectIteration" Path="IsOpen"/>
<Binding ElementName="chkIteration" Path="IsChecked"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="loc:MainMediator.IterationSelected" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
最后,我有这个 Grid
,它的可见性取决于属性。
<Grid Visibility="{Binding IterationSelected, Converter={StaticResource BooleanToVisibilityConverter}, diagnostics:PresentationTraceSources.TraceLevel=High}" Grid.Row="1">
<TextBlock>Testing 1323</TextBlock>
</Grid>
所以,这就是问题所在,我使用了:diagnostics:PresentationTraceSources.TraceLevel="High"
来测试所有绑定(bind)并且一切正常,除了当 loc:MainMediator:IterationSelected
设置为 true,Grid
不会拾取它。一点都不火。并不是说它不能绑定(bind)或类似的东西。它甚至不尝试。 Grid
绑定(bind)唯一一次触发是在应用程序启动时。它会在那时正确地触发和评估。
我唯一尝试过(但失败了)的是使 IterationSelected
成为附加属性,而不是普通的依赖属性。然而,那也失败了。
WPF 大师,有什么想法吗?
最佳答案
这有点难说,但看起来你的绑定(bind)源是错误的。属性引用似乎也存在一些歧义,但这可能是附加属性实验的产物。
基本上,您的 Grid.Visibility 绑定(bind)正在寻找当前 DataContext
上的 IterationSelected
属性。但是您的样式 DataTrigger
正在设置 Grid 上的 loc:MainMediator
.IterationSelected
属性。
解决此问题的方法是让您的 Grid.Visibility 绑定(bind)在 Grid 本身(而不是 数据上下文
)。为此,将 RelativeSource={RelativeSource Self}
添加到绑定(bind)声明中:
Visibility="{Binding IterationSelected,
Converter={StaticResource BooleanToVisibilityConverter},
RelativeSource={RelativeSource Self}}">
这告诉绑定(bind)查看它绑定(bind)到的 元素 而不是 DataContext
。
您可能还需要将路径更改为 loc:MainMediator.IterationSelected
而不仅仅是 IterationSelected
。
最后,根据 IterationSelected
属性是否正在执行任何其他工作,另一种方法是让您的 Setter 直接设置可见性:
<DataTrigger Value="True">
<!-- binding omitted -->
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
关于c# - wpf - 更新的依赖属性不触发绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2115307/