首页 > 解决方案 > C# bin new Textblock 对象到 XAML 文件中的 Textblock 控件

问题描述

我想在我的 C#-WPF 应用程序中显示一个包含链接的文本。这些文本是静态的并且在编译期间是已知的。

以下是我在直接处理 XAML 文件时想要的:

       <TextBlock Name="TextBlockWithHyperlink">
                Some text 
                <Hyperlink 
                    NavigateUri="http://somesite.com"
                    RequestNavigate="Hyperlink_RequestNavigate">
                    some site
                </Hyperlink>
                some more text
      </TextBlock>

由于使用 MVVM,我想通过依赖属性将 Textblock 绑定到新构造的 Textblock 对象。XAML 然后看起来像这样:

        <StackPanel Grid.Row="1" Margin="5 0 0 0">
            <TextBlock Height="16" FontWeight="Bold" Text="Generic Text with link"/>
          
            <TextBlock Text="{Binding Path=TextWithLink, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>

在我的 ViewModel 我放置

private void someMethod(){
   ...
   TextWithLink = CreateText();
   ...
}

private TextBlock(){
   TextBlock tb = new TextBlock();     
   Run run1 = new Run("Text preceeding the hyperlink.");
   Run run2 = new Run("Text following the hyperlink.");
   Run run3 = new Run("Link Text.");

   Hyperlink hyperl = new Hyperlink(run3);
   hyperl.NavigateUri = new Uri("http://search.msn.com");
   

   tb.Inlines.Add(run1);
   tb.Inlines.Add(hyperl);
   tb.Inlines.Add(run2);
   return tb;
}

private TextBlock _textWithLink;
public TextBlock TextWithLink { 
  get => _textWithLink;
  set{
    _textWithLink = value; 
    OnPropertyChanged();
  }
}

依赖属性设置工作我看到一个新的 TextBlock 被分配给 XAML 控件,但是没有显示任何内容,只是显示的文本读取

System.Windows.Controls.TextBlock

而不是内容。我无法理解我必须更改的内容以显示所需的混合文本。乐于助人。

标签: c#wpfmvvmtextblock

解决方案


与其在视图模型中使用 TextBlock 实例,Inline不如使用带有 UI 元素的元素集合,该 UI 元素接受它作为 Binding 的源。

由于 TextBlock 的 Inlines 属性不可绑定,因此您可以创建一个带有可绑定属性的 deribed TextBlock,如下所示:

public class MyTextBlock : TextBlock
{
    public static readonly DependencyProperty BindableInlinesProperty =
        DependencyProperty.Register(
            nameof(BindableInlines),
            typeof(IEnumerable<Inline>),
            typeof(MyTextBlock),
            new PropertyMetadata(null, BindableInlinesPropertyChanged));

    public IEnumerable<Inline> BindableInlines
    {
        get { return (IEnumerable<Inline>)GetValue(BindableInlinesProperty); }
        set { SetValue(BindingGroupProperty, value); }
    }

    private static void BindableInlinesPropertyChanged(
        DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var textblock = (MyTextBlock)o;
        var inlines = (IEnumerable<Inline>)e.NewValue;

        textblock.Inlines.Clear();

        if (inlines != null)
        {
            textblock.Inlines.AddRange(inlines);
        }
    }
}

现在你可以像这样使用它

<local:MyTextBlock BindableInlines="{Binding SomeInlines}"/>

具有这样的视图模型属性:

public IEnumerable<Inline> SomeInlines { get; set; }

...

var link = new Hyperlink(new Run("Search"));
link.NavigateUri = new Uri("http://search.msn.com");
link.RequestNavigate += (s, e) => Process.Start(e.Uri.ToString());

SomeInlines = new List<Inline>
{
    new Run("Some text "),
    link,
    new Run(" and more text")
};

推荐阅读