【问题标题】:Binding placeholder from resource file in WPF从 WPF 中的资源文件绑定占位符
【发布时间】:2015-05-29 17:32:08
【问题描述】:

在我的资源文件中

LOCKER NO. {0} IS OPEN

我需要将资源文件绑定到一个文本框,并希望在前景为红色的 palce holder 中动态设置值。

下面是我的代码

   <TextBlock x:Name="Title" Margin="0,70,0,0"
               HorizontalAlignment="Center"
               VerticalAlignment="Top"
               FontSize="42"
               FontWeight="SemiBold"
               Foreground="#888888"
     >
        <TextBlock.Text>
            <MultiBinding  StringFormat="{x:Static prop:Resources.LockerNumberIsOpen}">
                <Binding  Path="PrefixWithNumber"/>

            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

如何将 {0} 中的值显示为红色?

从后面的代码我们可以做到

Title.Text = string.Format(Properties.Resources.LockerNumberIsOpen, (this.DataContext as OpenParcelViewModel).PrefixWithNumber);
            var tr = this.Find((this.DataContext as OpenParcelViewModel).PrefixWithNumber);
            tr.ApplyPropertyValue(TextElement.ForegroundProperty, "#9ebb2b");

 private TextRange Find(string w)
        {
            var si = Title.Text.IndexOf(w);
            var sp = Title.ContentStart.GetPositionAtOffset(si + 1);
            var ep = Title.ContentStart.GetPositionAtOffset(si + w.Length + 1);
            return new TextRange(sp, ep);
        }

如何在 XAML 中做到这一点

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您需要使用richTextBox。这将允许您为块内的文本设置不同的颜色,但我不知道如何绑定它。 也许使用一种方法来创建一个具有所需格式的资源的对象,然后将其绑定到该对象。

    在 RichtextBox 中设置单独的颜色this question has it in detail.

    【讨论】:

    • 我使用多重绑定作为占位符,但将前景色设为红色仍然是个问题。注意:编辑问题
    猜你喜欢
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2014-01-01
    • 2018-07-18
    • 1970-01-01
    • 2014-12-09
    • 2013-02-02
    相关资源
    最近更新 更多