WPF TextBlock 绑定到 DependencyProperty

发布于 2024-07-23 06:46:18 字数 1426 浏览 2 评论 0原文

我认为这是尝试将视图绑定到视图模型中的依赖属性的最简单的情况之一。 似乎初始更改反映在视图中,但对 DP 的其他更改不会更新视图的 TextBlock。 我可能只是错过了一些简单的东西,但我只是看不出它是什么。 请看一下...

我的 XAML 在窗口底部有一个状态栏。 我想绑定到 DP“VRAStatus”。

        <StatusBar x:Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" 
               VerticalAlignment="Bottom" Background="LightBlue" Opacity="0.4" DockPanel.Dock="Bottom" >
            <StatusBarItem>
                <TextBlock x:Name="statusBar" Text="{Binding VRAStatus}" />
            </StatusBarItem>
            <StatusBarItem>
                <Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
            </StatusBarItem>
        </StatusBar>

我的视图模型定义了 DP:

    public string VRAStatus
    {
        get { return (string)GetValue(VRAStatusProperty); }
        set { SetValue(VRAStatusProperty, value); }
    }

    // Using a DependencyProperty as the backing store for VRAStatus. 
    public static readonly DependencyProperty VRAStatusProperty =
        DependencyProperty.Register("VRAStatus", typeof(string), typeof(PenskeRouteAssistViewModel),new PropertyMetadata(string.Empty));

然后,在我的代码中我设置了 DP:

        VRAStatus = "Test Message...";

这里是否有一些我遗漏的明显内容? 在我的视图模型构造函数中,我像这样设置了 DP:

        VRAStatus = "Ready";

我从来没有显示过测试消息。

I have what I believe to be about one of the most simple cases of attempting to bind a view to a dependencyproperty in the view model. It seems that the initial changes are reflected in the view but other changes to the DP do not update the view's TextBlock. I'm probably just missing something simple but I just can't see what it is. Please take a look...

My XAML has a status bar on the bottom of the window. I want to bind to the DP "VRAStatus".

        <StatusBar x:Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" 
               VerticalAlignment="Bottom" Background="LightBlue" Opacity="0.4" DockPanel.Dock="Bottom" >
            <StatusBarItem>
                <TextBlock x:Name="statusBar" Text="{Binding VRAStatus}" />
            </StatusBarItem>
            <StatusBarItem>
                <Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
            </StatusBarItem>
        </StatusBar>

My viewmodel has the DP defined:

    public string VRAStatus
    {
        get { return (string)GetValue(VRAStatusProperty); }
        set { SetValue(VRAStatusProperty, value); }
    }

    // Using a DependencyProperty as the backing store for VRAStatus. 
    public static readonly DependencyProperty VRAStatusProperty =
        DependencyProperty.Register("VRAStatus", typeof(string), typeof(PenskeRouteAssistViewModel),new PropertyMetadata(string.Empty));

Then, in my code I set the DP:

        VRAStatus = "Test Message...";

Is there something obvious here that I am missing? In my constructor for the viewmodel I set the DP like this:

        VRAStatus = "Ready";

I never get the Test Message to display.

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

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

发布评论

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

评论(5

青衫负雪 2024-07-30 06:46:18

您需要在 .

You need to add DataContext="{Binding RelativeSource={RelativeSource Self}} in .

著墨染雨君画夕 2024-07-30 06:46:18

事实证明,事情比我想象的要复杂一些(比如,什么时候不是这样的:) 我的 RibbonControl 位于 UserControl 中,用于从 MainWindow 中获取所有 XAML。 事实上,它位于 UserControl 中,这使得它与 ViewModel 的工作方式不同。 我不知道为什么——可能是那些永远无法解开的谜团之一。 但是,通过将 RibbonControl 直接放在 MainWindow 上,一切都会按预期工作 - 包括 DP 和 C# 属性。 有趣的。 (希望我能找回那两天的生活!)

谢谢,
账单

As it turns out things were a little more complicated than I had thought (like, when is that NOT the case :) My RibbonControl was in a UserControl to get all of that XAML out of the MainWindow. It was the fact that it was in a UserControl that made it work differently with the ViewModel. I don't know why - probably one of those mysteries that won't ever be solved. But by putting my RibbonControl directly on the MainWindow, everything works as expected - both with a DP and a C# Property. Interesting. (Wish I could get back those two days of my life!)

thanks,
Bill

空心↖ 2024-07-30 06:46:18

尝试在绑定中使用 Path 标志指定 DP 的名称,如下所示:

<TextBlock x:Name="statusBar" Text="{Binding Path=VRAStatus}">

Try specifying the name of the DP with the Path flag in the binding like this:

<TextBlock x:Name="statusBar" Text="{Binding Path=VRAStatus}">
枯叶蝶 2024-07-30 06:46:18

Bill,

您何时何地设置 DataContext? 我过去遇到过问题,当我在 InitializeComponent 之前设置 DataContext 时,我的 Bindings 从未正确执行。

另外,出于好奇:为什么在 ViewModel 中使用 DP 而不仅仅是属性?

Bill,

When and where do you set the DataContext? I had problems in the past that when I set the DataContext before the InitializeComponent, my Bindings never executed properly.

Also, for curiosity's sake: why do you use a DP in your ViewModel instead of just a Property?

深海不蓝 2024-07-30 06:46:18

尝试指定 Binding 的 UpdateSourceTrigger 属性:

<StatusBar x:Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" 
           VerticalAlignment="Bottom" Background="LightBlue" Opacity="0.4" DockPanel.Dock="Bottom" >
        <StatusBarItem>
            <TextBlock x:Name="statusBar" Text="{Binding VRAStatus, UpdateSourceTrigger=PropertyChanged}" />
        </StatusBarItem>
        <StatusBarItem>
            <Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
        </StatusBarItem>
    </StatusBar>

Try to specify the UpdateSourceTrigger property of the Binding:

<StatusBar x:Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" 
           VerticalAlignment="Bottom" Background="LightBlue" Opacity="0.4" DockPanel.Dock="Bottom" >
        <StatusBarItem>
            <TextBlock x:Name="statusBar" Text="{Binding VRAStatus, UpdateSourceTrigger=PropertyChanged}" />
        </StatusBarItem>
        <StatusBarItem>
            <Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
        </StatusBarItem>
    </StatusBar>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文