none
自定义依赖绑定问题 RRS feed

  • 问题

  • 我定义了一个这样的类

    public class Global : UserControl, INotifyPropertyChanged { public double MyFontSize { get { double value = (double)GetValue(MyFontSizeProperty); return value == 0 ? 1 : value; } set { SetValue(MyFontSizeProperty, value); MyPropertyChanged("MyFontSize"); } } public static readonly DependencyProperty MyFontSizeProperty = DependencyProperty.Register("MyFontSize", typeof(double), typeof(Global), null); public event PropertyChangedEventHandler PropertyChanged; private void MyPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } }

    在MainPage中

    public MainPage()
            {
                this.InitializeComponent();
                DataContext = this;
                t1.DataContext = g1;
                Binding b = new Binding() { Source = slider, Path = new PropertyPath("Value")};
                BindingOperations.SetBinding(muc1, MyUserControl1.MyFontSizeProperty, b);
                BindingOperations.SetBinding(g, Global.MyFontSizeProperty, b);
                BindingOperations.SetBinding(g1, Global.MyFontSizeProperty, b);
            }

            public static Global g = new Global();
            public Global g1 = new Global();

    然后Xaml中

     <StackPanel>
                <Slider Name="slider" Minimum="1" ValueChanged="slider_ValueChanged"></Slider>
                <local:MyUserControl1 x:Name="muc1"/>
                <TextBlock FontSize="{Binding MyFontSize}" Name="t1">Test FontSize</TextBlock>
                <local:Global/><!--删掉这句的话绑定Global的那两个字体大小将不会改变-->
            </StackPanel>


    问题就是xaml中如果有<local:Golbal/>这一句,绑定了g和g1的那两个textblock的字体大小会改变,但把<local:Golbal/>删掉后则不会改变

    完整代码在binding.zip里

    http://pan.baidu.com/s/1qXSanhy

    2016年5月28日 3:07