数据绑定的逻辑错误:属性似乎不会更新 [英] Logical Errors with data binding: a property seems not to update

查看:82
本文介绍了数据绑定的逻辑错误:属性似乎不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有这个XAML

 <  边框    CornerRadius   =  10   已移除  =  {Binding SelColor,Mode = OneWay}    BorderBrush   = 黑色    BorderThickness   =  1   保证金  =  7 >  
< 矩形 MinHeight = 20 MinWidth = 180 填充 = 透明 / >
< / Border >
< Expander 标题 = ChooseColor 扩展 = Expander_Expanded 名称 = Exp 已折叠 = Exp_Collapsed >
< StackPanel >
< StackPanel 方向 = 水平 保证金 = 2 >
< TextBlock 文字 = R 前景 = {Binding Foreground} / >
< 滑块 已移除 = {Dynami cResource RedSliderGrad} 最小 = 0 最大 = 255 MinWidth = 180 = {Binding SelColor。 Color.R,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} ValueChanged = Slider_ValueChanged / >
< / StackPanel >
< StackPanel 方向 = 水平 保证金 = 2 >
< TextBlock 文本 = G 前景 = {Binding Fore地面} / >
< Slider 已删除 = {DynamicResource GreenSliderGrad} 最小 = 0 最大 = 255 MinWidth = 180 < span class =code-keyword> = {Binding SelColo r.Color.G,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} ValueChanged = Slider_ValueChanged / >
< / StackPanel >
< StackPanel 方向 = 水平 保证金 = 2 >
< TextBlock 文本 = B 前景 = {Binding Foreground} / >
< 滑块 已移除 = < span class =code-keyword> {DynamicResource BlueSliderGrad} 最小 = 0 最大 = < span class =code-keyword> 255 MinWidth = 180 = {Binding SelColor.Color.B,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} ValueChanged = Slider_ValueChanged / >
< / StackPanel >
< / StackPanel >
< / Expander >
< / StackPanel >





和这个C#代码:



  public  SolidColorBrush SelColor 
{
get { return (的SolidColorBrush)的GetValue(SelColorProperty); }
set
{
SetValue(SelColorProperty, value );
if (PropertyChanged!= null
PropertyChanged( this new PropertyChangedEventArgs( SelColor));
}
}

// 使用DependencyProperty作为后盾商店为SelColor。这样可以实现动画,样式,装订等......
public static readonly DependencyProperty SelColorProperty =
DependencyProperty.Register( SelColor typeof (SolidColorBrush), typeof (ColorChooser), new PropertyMetadata( new SolidColorBrush(Color.FromArgb( 255 204 204 204 ))) );

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

private void Expander_Expanded( object sender,RoutedEventArgs e)
{
// this.Exp.IsExpanded = true;
// this.Height + = this.Exp .ActualHeight;
}

private void Exp_Collapsed( object sender,RoutedEventArgs e)
{
// this.Exp.IsExpanded = true;
// this .Height - = this.Exp.ActualHeight;
// this.Exp.IsE xpanded = false;
}

public event PropertyChangedEventHandler PropertyChanged;

private void Slider_ValueChanged(对象发​​件人,RoutedPropertyChangedEventArgs< double> e)
{

}





我注意到,随着滑块的变化,甚至SelColor也会发生变化,但是边框的背景并没有反映出这种变化......为什么?



谢谢!



Jymmy097

解决方案

颜色 SolidColorBrush struct ,而不是,所以你无法以与相同的方式绑定到它,因为当它被绑定引用时,它会得到<$ c> 的 SelColor.Color 来自 SolidColorBrush >颜色

Hi everybody,

I have this XAML

<Border CornerRadius="10" removed="{Binding SelColor, Mode=OneWay}" BorderBrush="Black" BorderThickness="1" Margin="7">
            <Rectangle MinHeight="20" MinWidth="180" Fill="Transparent" />
        </Border>
        <Expander Header="ChooseColor" Expanded="Expander_Expanded" Name="Exp" Collapsed="Exp_Collapsed">
            <StackPanel>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="R" Foreground="{Binding Foreground}"/>
                    <Slider removed="{DynamicResource RedSliderGrad}" Minimum="0" Maximum="255" MinWidth="180" Value="{Binding SelColor.Color.R, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ValueChanged="Slider_ValueChanged"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="G" Foreground="{Binding Foreground}"/>
                    <Slider removed="{DynamicResource GreenSliderGrad}" Minimum="0" Maximum="255" MinWidth="180" Value="{Binding SelColor.Color.G, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ValueChanged="Slider_ValueChanged"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="B" Foreground="{Binding Foreground}"/>
                    <Slider removed="{DynamicResource BlueSliderGrad}" Minimum="0" Maximum="255" MinWidth="180" Value="{Binding SelColor.Color.B, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ValueChanged="Slider_ValueChanged"/>
                </StackPanel>
            </StackPanel>
        </Expander>
    </StackPanel>



and this C# code:

public SolidColorBrush SelColor
        {
            get { return (SolidColorBrush)GetValue(SelColorProperty); }
            set
            {
                SetValue(SelColorProperty, value);
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("SelColor"));
            }
        }

        // Using a DependencyProperty as the backing store for SelColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelColorProperty =
            DependencyProperty.Register("SelColor", typeof(SolidColorBrush), typeof(ColorChooser), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 204, 204, 204))));

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

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            //this.Exp.IsExpanded = true;
            //this.Height += this.Exp.ActualHeight;
        }

        private void Exp_Collapsed(object sender, RoutedEventArgs e)
        {
            //this.Exp.IsExpanded = true;
            //this.Height -= this.Exp.ActualHeight;
            //this.Exp.IsExpanded = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

        }



I noticed that, as the slider changes, even the SelColor changes, but that changement is not reflected by the background of the border... Why?

Thanks!

Jymmy097

解决方案

The Color of a SolidColorBrush is a struct, not a class, so you can't bind to it the same way as a class, because when it is referenced by the binding, it gets a copy of the SelColor.Color from the SolidColorBrush, not the same actual instance of the Color.


这篇关于数据绑定的逻辑错误:属性似乎不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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