使用 Dependency 属性设置标签内容

发布于 2024-09-14 17:05:50 字数 1444 浏览 1 评论 0原文

我很难理解如何在 C# 和 xaml 代码之间使用依赖属性。 这是我的问题 XAML 代码的一个小代码示例

<Window x:Class="WpfChangeTextApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Label Name="statusTextLabel" Content="{Binding StatusText}"></Label>
    <Button Name="changeStatusTextButton" Click="changeStatusTextButton_Click">Change Text</Button>
</StackPanel>

C# 代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();            
    }

    public string StatusText
    {
        get { return (string)GetValue(StatusTextProperty); }
        set { SetValue(StatusTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StatusText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StatusTextProperty =
        DependencyProperty.Register("StatusText", typeof(string), typeof(MainWindow));

    private void changeStatusTextButton_Click(object sender, RoutedEventArgs e)
    {
        StatusText = "Button clicked";
    }
}

所以,我的问题是,当我单击按钮时,标签 statusTextLabel 不会更新。我的问题是,我不知道代码的哪一部分做错了,是在 xaml 中还是在 C# 中?在 xaml 中,我可能在绑定中做错了什么?或者我是否错过了在 C# 代码中执行某些操作?

I have trouble to understand how dependency properties can be used between C# and xaml code.
This is a smal code example of my question

XAML code:

<Window x:Class="WpfChangeTextApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Label Name="statusTextLabel" Content="{Binding StatusText}"></Label>
    <Button Name="changeStatusTextButton" Click="changeStatusTextButton_Click">Change Text</Button>
</StackPanel>

C# code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();            
    }

    public string StatusText
    {
        get { return (string)GetValue(StatusTextProperty); }
        set { SetValue(StatusTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StatusText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StatusTextProperty =
        DependencyProperty.Register("StatusText", typeof(string), typeof(MainWindow));

    private void changeStatusTextButton_Click(object sender, RoutedEventArgs e)
    {
        StatusText = "Button clicked";
    }
}

So, my trouble is that Label statusTextLabel dose not get updated when I click on the button. My trouble is that I don't know in what part of the code that I'm doing something wrong, is it in the xaml or in the C#? In the xaml I might doing something wrong in the Binding? Or have I missed doing something in the C# code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为一人 2024-09-21 17:05:50

默认情况下,绑定路径是相对于当前元素的 DataContext 属性的。您尚未将其设置为任何内容,因此它无法解析绑定。如果您想要窗口类上的 StatusText 属性,那么有两种方法。一种是使用与FindAncestor的RelativeSource的绑定来查找树中的Window并直接绑定到其属性:

<Label Name="statusTextLabel" Content="{Binding StatusText, 
    RelativeSource={RelativeSource AncestorType=Window}}"></Label>

另一种是将Window的DataContext设置为其自身,因此它将被标签继承。例如,在构造函数中:

public MainWindow()
{
    this.DataContext = this;
    InitializeComponent();
}

对于大多数应用程序,您实际上需要一个单独的类来表示数据,并且您将将该类的实例设置为 DataContext。您还可以使用普通的 CLR 属性而不是依赖项属性,但如果您希望在属性更改时通知 UI,则需要实现 INotifyPropertyChanged。当您编写自定义控件并且希望用户能够使用数据绑定设置属性时,依赖属性更有用。

By default, binding paths are relative to the DataContext property of the current element. You have not set it to anything, so it can't resolve the binding. If you want the StatusText property on your window class, then there are two approaches. One is to use a binding with a RelativeSource of FindAncestor to find the Window in the tree and bind to its properties directly:

<Label Name="statusTextLabel" Content="{Binding StatusText, 
    RelativeSource={RelativeSource AncestorType=Window}}"></Label>

The other is to set the DataContext of the Window to itself, so it will be inherited by the label. For example, in your constructor:

public MainWindow()
{
    this.DataContext = this;
    InitializeComponent();
}

For most applications, you will actually want a separate class to represent the data, and you will set an instance of that class as the DataContext. You can also use ordinary CLR properties instead of dependency properties, although you will need to implement INotifyPropertyChanged if you want to UI to be informed when properties change. Dependency properties are more useful when you are writing a custom control and you want users to be able to set the properties using data binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文