Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C bin将新的Textblock对象添加到XAML文件中的Textblock控件_C#_Wpf_Mvvm_Textblock - Fatal编程技术网

C# C bin将新的Textblock对象添加到XAML文件中的Textblock控件

C# C bin将新的Textblock对象添加到XAML文件中的Textblock控件,c#,wpf,mvvm,textblock,C#,Wpf,Mvvm,Textblock,我想在我的C-WPF应用程序中显示一个包含链接的文本。文本是静态的,在编译时是已知的 以下是直接处理XAML文件时我想要的: <TextBlock Name="TextBlockWithHyperlink"> Some text <Hyperlink NavigateUri="http://somesite.com"

我想在我的C-WPF应用程序中显示一个包含链接的文本。文本是静态的,在编译时是已知的

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

       <TextBlock Name="TextBlockWithHyperlink">
                Some text 
                <Hyperlink 
                    NavigateUri="http://somesite.com"
                    RequestNavigate="Hyperlink_RequestNavigate">
                    some site
                </Hyperlink>
                some more text
      </TextBlock>
dependency属性设置正在运行,我看到一个新的TextBlock被分配给XAML控件,但是没有显示内容,只有显示的文本读取

System.Windows.Controls.TextBlock


而不是内容。为了显示所需的混合文本,我无法理解必须更改的内容。很高兴得到帮助。

您不应该在视图模型中使用TextBlock实例,而应该使用一组内联元素和一个接受它作为绑定源的UI元素

由于TextBlock的Inlines属性是不可绑定的,因此可以使用如下可绑定属性创建派生TextBlock:

        <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>
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);
        }
    }
}
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")
};
现在你可以像这样使用它

<local:MyTextBlock BindableInlines="{Binding SomeInlines}"/>
使用如下视图模型属性:

        <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>
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);
        }
    }
}
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")
};

set=>OnPropertyChanged;显然没有设置属性的支持字段。它应该设置为{u textWithLink=value;OnPropertyChanged;}。除此之外,您不会在视图模型中使用TextBlock实例。如前所述,DepProp的设置不是问题所在,这在发布问题时被遗忘了。为什么我不在Vm中使用TextBlock obj?因为它是一个UI元素,即视图类。搜索StackOverflow,了解如何将XAML中TextBlock的Inlines属性设置为InlinesCollection。虽然内联线不是依赖性属性,因此不可直接绑定,但有一些变通方法,例如使用附加属性或派生的TextBlock类。这:仅供参考,所选绑定参数Mode=OneWay和UpdateSourceTrigger=PropertyChanged是互斥的。由于使用MVVM,我想通过依赖属性将Textblock绑定到新构造的Textblock对象您真的了解MVVM、绑定和DP的概念吗?下面是我在直接处理XAML文件时想要做的事情——没有任何合理的理由来更改它。