WPF基于属性向TextBlock添加文本

时间:2018-01-05 15:58:41

标签: c# wpf textblock

我有一个ListView,其中有一列显示状态两个字母的缩略图。  基于布尔属性(HasWorkState)的值,我想在HasWorkState = false时在州缩写周围添加括号。

例如:

  • HasWorkState = true显示OH
  • HasWorkState = false显示(OH)

州名缩写显示在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>

我知道在ToolTip时显示HasWorkState = false时正确应用了此样式。我只需要知道如何添加括号。我目前对Text值设置器的作用不起作用。

2 个答案:

答案 0 :(得分:2)

你有一个解决方案,但是值得知道你遇到问题的原因:因为你在TextBlock元素上设置了Text属性,所以样式不能覆盖它。如果您使用样式设置器应用默认的{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>

答案 1 :(得分:1)

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

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