English 中文(简体)
数据链
原标题:Chain of DataBinding

我在努力遵循数据集。

Property -> DependencyProperty -> Property

But i have trouble. For example, We have simple class with two properties implements INotifyPropertyChanged:

public class MyClass : INotifyPropertyChanged
    {
        private string _num1;
        public string Num1
        {
            get { return _num1; }
            set
            {
                _num1 = value;
                OnPropertyChanged("Num1");
            }
        }

        private string _num2;
        public string Num2
        {
            get { return _num2; }
            set
            {
                _num2 = value;
                OnPropertyChanged("Num2");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(e));
        }
    }

And textBlock用xaml宣布:

<TextBlock Name="tb" FontSize="20" Foreground="Red" Text="qwerqwerwqer" />

现在允许试图约束Num1。 案文:

private MyClass _myClass = new MyClass();
        public MainWindow()
        {
            InitializeComponent();

            Binding binding1 = new Binding("Num1")
                                   {
                                       Source = _myClass, 
                                       Mode = BindingMode.OneWay
                                   };

            Binding binding2 = new Binding("Num2")
            {
                Source = _myClass,
                Mode = BindingMode.TwoWay
            };

            tb.SetBinding(TextBlock.TextProperty, binding1);

            //tb.SetBinding(TextBlock.TextProperty, binding2);


            var timer = new Timer(500) {Enabled = true,};

            timer.Elapsed += (sender, args) => _myClass.Num1 += "a";

            timer.Start();


        }

它运作良好。 但是,如果我们不赞成这一扼杀。

tb.SetBinding(TextBlock.TextProperty, binding2);

案文Block没有显示任何东西。 Data work 如何做我想要的东西?

最佳回答

问题是,<条码>约束明确了以前的任何约束。 因此,当你对<代码>Num2具有约束力时,你将排除对<代码>Num1的约束。 之所以出现这种情况,是因为受扶养财产约束不能有多种来源,它知道如何使用? (当然,这忽视了<代码>MultiBled的用法,但这不能在这种情形下帮助你。)

您可以这样做的方式是制作<代码>MyClass a DependencyObjectNum1Num2附属性质。 然后,如果案文从收到最新资料,将予以更新。

照片价值1 000字,你在左边看到了。 你们需要做的是:

http://img339.imageshack.us/img339/448/2sources.png”

决心尽力确保我的逻辑是正确的,实际上它行之有效,但有一些骗局。 对开端人来说,这里是新的<代码>。 MyClass Code:

public class MyClass : FrameworkElement
{
    public static readonly DependencyProperty Num1Property =
        DependencyProperty.Register("Num1", typeof(string), typeof(MyClass));

    public static readonly DependencyProperty Num2Property =
        DependencyProperty.Register("Num2", typeof(string), typeof(MyClass));

    public string Num1
    {
        get { return (string)GetValue(Num1Property); }
        set { SetValue(Num1Property, value); }
    }

    public string Num2
    {
        get { return (string)GetValue(Num2Property); }
        set { SetValue(Num2Property, value); }
    }
}

这里的任何术语只是用<代码>代谢/代码”替换。 现在,请从窗户代码背后检查:

public partial class DataBindingChain : Window
{
    public MyClass MyClass
    {
        get;
        set;
    }

    public DataBindingChain()
    {
        MyClass = new MyClass();

        InitializeComponent();

        Binding binding1 = new Binding("Num1")
        {
            Source = MyClass,
            Mode = BindingMode.OneWay
        };

        Binding binding2 = new Binding("Text")
        {
            Source = tb,
            Mode = BindingMode.OneWay
        };

        tb.SetBinding(TextBlock.TextProperty, binding1);
        MyClass.SetBinding(MyClass.Num2Property, binding2);

        var timer = new Timer(500) { Enabled = true, };

        timer.Elapsed += (sender, args) => Dispatcher.Invoke(UpdateAction, MyClass);

        timer.Start();
    }

    Action<MyClass> UpdateAction = (myClass) => { myClass.Num1 += "a"; };
}

这是发生魔鬼的地方:我们设立了两个具有约束力的机构。 第一种对<代码>TextBlock.Text至Num1具有约束力,第一种对<代码>Num2至TextBlock具有约束力。 文本。 现在,我们有像我所显示的情况右侧一样的情景,即数据约束链。 另一种魔法是,我们无法更新Num1,其含义不同于其创建时的面目,从而形成一种相互接近的例外。 为绕过这一点,我们只是利用<代码>Displer<>/code>向ID thread援引最新情况。

最后,XAML用于示范:

<Window x:Class="TestWpfApplication.DataBindingChain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataBindingChain" Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock Name="tb" Grid.Row="0" FontSize="20" Foreground="Red"/>
    <TextBlock Name="tb2" Grid.Row="1" FontSize="20" Foreground="Blue" Text="{Binding MyClass.Num2}"/>
</Grid>

And voila! 制成品:

alt text http://img163.imageshack.us/img163/6114/victorynf.png

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...