首页 > 解决方案 > WPF 下划线绑定 (MVVM)

问题描述

有没有办法绑定下划线?我正在尝试实现以下目标:

我有VievModel一个 bool 属性:
public bool HomeButtonUnderline { get; set; } = false;

然后我想在以下函数中控制这个属性:

public void Home() {
//CurrentPage = ApplicationPage.Home;
//HomeButtonForeground = new SolidColorBrush(Colors.White);
HomeButtonUnderline = true;
SettingsButtonUnderline = false;
}

然后我可以在 XAML 中使用这个控件:

<Button>
<TextBlock Command="{Binding HomeNavCommand}" Underline="{Binding HomeButtonUnderline}"/> 
</Button>

问题是没有“下划线”属性,而是由“TextDecorations”处理:

<Button>
<TextBlock TextDecorations="Underline">
</Button>

那么有没有办法使用 MVVM 控制下划线,甚至不用它?

标签: wpfmvvmdata-binding

解决方案


您可以在 TextBlock 的样式中使用 DataTrigger:

<Button Command="{Binding HomeNavCommand}">
    <TextBlock Text="Home">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding HomeButtonUnderline}" Value="True">
                        <Setter Property="TextDecorations" Value="Underline"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</Button>

推荐阅读