WPF DataBinding来控制属性 [英] WPF DataBinding to control properties

查看:70
本文介绍了WPF DataBinding来控制属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,即我没有通过绑定接收更新。



我有一个标签,它通过DataContext绑定到TextBox属性的ExtentWidth。



我的绑定最初工作,并在标签中显示值0但是在此之后它不会更新。



ExtentWidth是一个只读属性,我不确定是否这会以任何方式影响绑定,但我有一个标签,当它被设置时绑定到文本,所以我知道它可以接收更新。 (按钮更新文字和标签更新)



下面是一些代码来证明我的问题。



I have an issue whereby I am not receiving updates through my bindings.

I have a label which is bound to the ExtentWidth of the TextBox property via the DataContext.

My binding initially works and displays the value of 0 in the label however it does not update after this.

ExtentWidth is a read only property, I'm not sure if this affects the binding in any way but I have a label the binds to the text when it is set so I know it can receive updates. (button updates text and label is updated)

below is some code to demonstrate my issue.

<Window x:Class="TestHarnesses.Views.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <ContentPresenter x:Name="ContentPresenter" Content="{Binding}"></ContentPresenter>
            <Label x:Name="lblExtentWidth" 

                   Content="{Binding ExtentWidth, Mode=OneWay}"/>
            <Label x:Name="lblText" 

                   Content="{Binding Text, Mode=OneWay}"/>
            <Button Content="Different Jibber Jabber" Click="ButtonBase_OnClick"/>
        </StackPanel>
    </Grid>
</Window>







using System.Windows;
using System.Windows.Controls;

namespace TestHarnesses.Views
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            TextBox tb = new TextBox(){Text = "Jibber Jabber"};
            this.TestTextBox = tb;
        }



        public TextBox TestTextBox
        {
            get { return (TextBox)GetValue(TestTextBoxProperty); }
            set { SetValue(TestTextBoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TestTextBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TestTextBoxProperty =
            DependencyProperty.Register("TestTextBox", typeof(TextBox), typeof(Window1), new PropertyMetadata(OnTestTextBoxProperty));

        private static void OnTestTextBoxProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Window1) d).DataContext = (TextBox) e.NewValue;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            TestTextBox.Text = "Different Jibber Jabber";
        }
    }
}

推荐答案

TextBoxBase.ExtentWidth [ ^ ]不是 DependencyProperty [ ^ ]。您可以绑定它,但是当值发生变化时,没有任何方法可以通知绑定。
TextBoxBase.ExtentWidth[^] is not a DependencyProperty[^]. You can bind to it, but there isn't any way for the binding to be notified when the value changes.


这篇关于WPF DataBinding来控制属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆