WPF 添加文本到基于 属性 的 TextBlock

WPF Add text to TextBlock based on property

我有一个 ListView,其中有一列显示州两个字母的评价。 基于布尔值 属性 (HasWorkState) 我想在 HasWorkState = false.

时在州缩写周围添加括号

例如:

州缩写显示在GridViewColumn:

<!-- Work State -->
<GridViewColumn Width="{Binding ActualWidth, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, 
                          Converter={StaticResource MathConverter}, ConverterParameter=(x/10)*0.5}">
    <GridViewColumn.Header>
        <GridViewColumnHeader Content=" State"
                              HorizontalContentAlignment="Left" />
    </GridViewColumn.Header>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding WorkState}"
                       Style="{StaticResource StateStyle}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

为此GridViewColumn我应用了这种风格:

<!-- Highlight missing work states -->
<Style x:Key="StateStyle" TargetType="TextBlock">
    <Style.Triggers>
        <DataTrigger Binding="{Binding HasWorkState}" Value="False">
            <Setter Property="Text" Value="{Binding StringFormat=({0})}" />
            <Setter Property="ToolTip" Value="Work location unspecified. Using residence state." />
        </DataTrigger>
    </Style.Triggers>
</Style>

我知道此样式应用正确,因为 ToolTipHasWorkState = false 时显示。我只需要知道如何添加括号。我目前的 Text 值 setter 不起作用。

最好的方法是将 属性 添加到您的视图模型中。在你的视图模型中是这样的:

internal class StateStyleViewModel : INotifyPropertyChanged
{
    private bool hasWorkState;
    private string workState;
    public event PropertyChangedEventHandler PropertyChanged;

    private string WorkState
    {
        get { return workState; }
        set
        {
            if (value == workState) return;
            workState = value;
            OnPropertyChanged();
            //Notify that the value of the dependent property has changed.
            OnPropertyChanged(nameof(DisplayWorkState));
        }
    }

    public bool HasWorkState
    {
        get { return hasWorkState; }
        set
        {
            if (value == hasWorkState) return;
            hasWorkState = value;
            OnPropertyChanged();
            //Notify that the value of the dependent property has changed.
            OnPropertyChanged(nameof(DisplayWorkState));
        }
    }

    /// <summary>
    /// Property for binding
    /// </summary>
    public string DisplayWorkState => HasWorkState ? WorkState : $"({WorkState})";

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后您只需绑定到 DisplayWorkState 而不是 WorkState

您已经找到了解决方案,但值得了解您遇到问题的原因:因为您在 TextBlock 元素上设置 Text 属性,所以样式无法覆盖它。如果您使用样式 setter 来应用默认值 {Binding WorkState},该样式将能够覆盖它。

我更喜欢在视图中保留这样的东西,所以我的方法是这样的:

<ContentControl Content="{Binding WorkState}" Style="{StaticResource StateStyle}" />

状态样式:

<Style x:Key="StateStyle" TargetType="ContentControl">
    <Style.Triggers>
        <DataTrigger Binding="{Binding HasWorkState}" Value="False">
            <!-- Leave the content alone and just change the format string -->
            <Setter Property="ContentStringFormat" Value="({0})" />

            <Setter 
                Property="ToolTip" 
                Value="Work location unspecified. Using residence state." 
                />
        </DataTrigger>
    </Style.Triggers>
</Style>