如何使用多个依赖项属性绑定到计算属性



我正在尝试构建一个可以在整个项目中重用的 WPF 用户控件。基本上是这样的:

  • 水平线(称为顶线(
  • 文本块 - 水平线(底线(

当窗口调整大小并且太小时,我只显示底线以节省空间。所以我希望有这些选择:

  • 隐藏所有行
  • 仅显示顶行
  • 仅显示底线

我使用三个依赖项属性来间接设置每行的可见性:

  • 显示线条(启用线条(
  • 可以压缩(启用显示顶部或底部,具体取决于可用空间(
  • 显示
  • 压缩(为真时显示底线,假时显示顶线(

直接绑定到依赖项属性有效。间接不会。

现在的问题是我是否需要使用:

  1. 像 FrameworkPropertyMetadataOptions.AffectsRender for FrameworkPropertyMetadata 这样的标志
  2. PropertyChangedCallback for DependencyProperty.Register 创建 FrameworkPropertyMetadata(英语:FrameworkPropertyMetadata(
  3. 价值转换器
  4. 不定属性已更改

我试过什么

我尝试了上述所有四个选项,但我还不够了解它,无法让一个工作。

我创建了从三个依赖项属性返回计算值的普通属性。它们使用 DependencyProperty.Register 设置的默认值,它们不会更新,甚至不使用父用户控件中设置的值。

当前状态

<StackPanel>
<Border Height="1"
Background="Black"
Margin="0 10 0 0"
Visibility="{Binding ShowTopLine, ElementName=Root, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding Text, ElementName=Root, Mode=TwoWay, FallbackValue=Heading}"
Margin="0 10 10 0" />
<Border Grid.Column="1" 
Height="1" 
Margin="0 10 0 0" 
Background="Black" 
VerticalAlignment="Center" 
Visibility="{Binding ShowBottomLine, ElementName=Root, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>
</StackPanel>

代码隐藏的相关部分,显示三个依赖项属性之一:

internal partial class Heading
{
public bool ShowBottomLine => ShowLine && (!CanCompress || ShowCompressed);
public static readonly DependencyProperty CanCompressProperty = GetRegisterProperty("CanCompress", typeof(bool), true);
public bool CanCompress
{
get => (bool) GetValue(CanCompressProperty);
set => SetValue(CanCompressProperty, value);
}
// Same for other two properties
public static DependencyProperty GetRegisterProperty(string name, Type type, object defaultValue)
{
return DependencyProperty.Register(name, type, typeof(Heading), new FrameworkPropertyMetadata(defaultValue));
}
}

如何在另一个用户控件中使用标题:

<local:Heading Text="{Binding HeaderText}"
CanCompress="False" 
ShowLine="False"/>

我该如何从这里继续?我知道放弃计算的属性很简单,但这意味着我需要在其他地方计算它们的状态。我想在标题中完成所有操作。

主要问题似乎是计算的属性不会强制刷新。这是一个公平的总结吗?

您可以通过定义ControlTemplate和简单的触发器来实现此逻辑:

<UserControl x:Class="ExampleControl">
<UserControl.Template>
<ControlTemplate TargetType="ExampleControl">
<StackPanel>
<Border x:Name="TopLine"
Background="Black" 
Height="1"
Margin="0 10 0 0" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, FallbackValue=Heading}"
Margin="0 10 10 0" />
<Border x:Name="BottomLine"
Grid.Column="1"
Height="1"
Margin="0 10 0 0"
Background="Black"
VerticalAlignment="Center" />
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="ShowLines" Value="False">
<Setter TargetName="TopLine" Property="Visibility" Value="Collapsed" />
<Setter TargetName="BottomLine" Property="Visibility" Value="Collapsed" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="CanCompress" Value="True" />
<Condition Property="ShowCompressed" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="TopLine"
Property="Visibility"
Value="Collapsed" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="CanCompress" Value="True" />
<Condition Property="ShowCompressed" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="BottomLine"
Property="Visibility"
Value="Collapsed" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
</UserControl>

或者(建议(使用自定义控件(扩展Control(。

最新更新