如何在关联的XAML中获取C#属性?

如何在关联的XAML中获取C#属性?,c#,wpf,xaml,C#,Wpf,Xaml,我有这样一个C#类: public partial class MyClass: UserControl { public String SomeText{get;set;} ... public MyClass(String Text) { this.InitializeComponent(); SomeText = Text; } } 我想在我的XAML文件中获取属性SomeText。怎么做这样的事 <Tex

我有这样一个C#类:

public partial class MyClass: UserControl
{
    public String SomeText{get;set;}
    ...

    public MyClass(String Text)
    {
        this.InitializeComponent();
        SomeText = Text;
    }

}
我想在我的XAML文件中获取属性
SomeText
。怎么做这样的事

<TextBlock Text="{THIS IS HERE I WANT TO HAVE SomeText}"></TextBlock>


我是C#的新手,所以我不知道该怎么做。必须有一个简单的方法?

首先,纠正:

这是一个
属性
,而不是
属性
,这是两个完全不同的概念

第二:

WPF有一个称为
dependencProperties
的概念,为了在WPF中构建复杂的自定义UI控件,必须利用这个概念

第三:

有时,您实际上不需要在UI中声明属性。WPF使您能够通过其强大的。因此,请再次考虑您正在声明的控件是否是声明字符串属性的正确位置

如果你打算在WPF工作,我建议你熟悉所有这些概念

给我更多关于你想做什么的细节,我可以给你更多的洞察力

编辑:

基本上,您需要做的是在控件中声明一个
dependencProperty
,如下所示:

public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register("DisplayName", typeof(string), typeof(mycontrol));

public string DisplayName
{
    get { return GetValue(DisplayNameProperty).ToString(); }
    set { SetValue(DisplayNameProperty, value); }
}
然后,在XAML中:

<TextBlock Text="{Binding DisplayName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>


请记住,WPF需要一种与其他框架完全不同的思维方式,因此我的建议仍然有效:如果您打算认真使用WPF,请熟悉MVVM和数据绑定。

首先,纠正一下:

这是一个
属性
,而不是
属性
,这是两个完全不同的概念

第二:

WPF有一个称为
dependencProperties
的概念,为了在WPF中构建复杂的自定义UI控件,必须利用这个概念

第三:

有时,您实际上不需要在UI中声明属性。WPF使您能够通过其强大的。因此,请再次考虑您正在声明的控件是否是声明字符串属性的正确位置

如果你打算在WPF工作,我建议你熟悉所有这些概念

给我更多关于你想做什么的细节,我可以给你更多的洞察力

编辑:

基本上,您需要做的是在控件中声明一个
dependencProperty
,如下所示:

public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register("DisplayName", typeof(string), typeof(mycontrol));

public string DisplayName
{
    get { return GetValue(DisplayNameProperty).ToString(); }
    set { SetValue(DisplayNameProperty, value); }
}
然后,在XAML中:

<TextBlock Text="{Binding DisplayName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>


请记住,WPF需要一种与其他框架完全不同的思维方式,因此我的建议仍然有效:如果您打算认真使用WPF,请熟悉MVVM和数据绑定。

UserControl
元素一个
名称
,然后像下面的示例那样绑定到它。如果您的文本不会更改,您应该在调用
InitialiseComponent
之前定义它

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="root">
    <Grid>
        <TextBlock Text="{Binding SomeText,ElementName=root}"/>
    </Grid>
</UserControl>
然后,您应该将绑定更改为如下所示,当您更改
SomeString
的值时,这将导致UI更新

<TextBlock Text="{Binding SomeText,ElementName=root,UpdateSourceTrigger=PropertyChanged}"/>

UserControl
元素一个
名称
,然后像下面的例子那样绑定到它。如果您的文本不会更改,您应该在调用
InitialiseComponent
之前定义它

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="root">
    <Grid>
        <TextBlock Text="{Binding SomeText,ElementName=root}"/>
    </Grid>
</UserControl>
然后,您应该将绑定更改为如下所示,当您更改
SomeString
的值时,这将导致UI更新

<TextBlock Text="{Binding SomeText,ElementName=root,UpdateSourceTrigger=PropertyChanged}"/>


这并不能回答问题;尽管篇幅很长,但它应该是一篇评论。也许它没有回答,但它澄清了一些概念,所以HigCore感谢您写这篇文章!这并不能回答问题;尽管篇幅很长,但它应该是一篇评论。也许它没有回答,但它澄清了一些概念,所以HigCore感谢您写这篇文章@皮格罗,不客气。知道键入
propdp
并点击tab两次(如果您有resharper,则点击一次)将为您生成大量依赖项属性代码可能会很有用。@Pigrou,不客气。知道键入
propdp
并点击两次tab(如果您有resharper,则点击一次)将为您生成大量依赖项属性代码可能很有用。