简体   繁体   English

边框背景绑定只工作一次

[英]Border background binding works only once

I have a border with background binded to the color property in my ViewModel.我有一个背景绑定到我的 ViewModel 中的颜色属性的边框。 But background change color only once after initialization.但是背景在初始化后只改变一次颜色。 At the same time, I have 3 textboxes binded to the same property(R, G and B) and they works fine.同时,我有 3 个文本框绑定到相同的属性(R、G 和 B)并且它们工作正常。

Why is textboxes works fine and border not?为什么文本框可以正常工作而边框不行? Here is border's code:这是边框的代码:

<Border x:Name="bNewColor" BorderBrush="Black" >
    <Border.Background>
        <SolidColorBrush Color="{Binding NewColor}"/>
    </Border.Background>    
</Border>

Textboxes:文本框:

<Ellipse x:Name="eMarker" Margin="{Binding MarkerMargin, Mode=TwoWay}" Width="6" Height="6"/>
<TextBox x:Name="tbColorR" TextWrapping="Wrap" Text="{Binding NewColor.R}"/>

VM:虚拟机:

class ViewModel
{
    Palette _palette
    Thickness _markerMargin;

    public Thickness MarkerMargin
    {
        get { return _markerMargin; }
        set
        {
            _markerMargin = value;
            _palette.DeterminateColorInPoint((int)_markerMargin.Left, (int)_markerMargin.Top);
            OnPropertyChanged();
        }
    }

    public ViewModel()
    {
        _palette = new Palette();
    }

...
}

Palette class:调色板类:

class Palette
{
   Color[,] _paletteColors;
   Color _newColor;
   public Color NewColor
   {
       get { return _newColor; }
       set
       {
           _newColor = value;
           OnPropertyChanged();
       }
   }

    public void DeterminateColorInPoint(int x, int y)
    {
        _newColor = _paletteColors[x, y];
    }

...
}

As I can see your DeterminateColorInPoint(int x, int y) method doesn't raise the OnPropertyChanged() of a NewColor property, thus XAML won't never know that the NewColor was changed.正如我所看到的,您的 DeterminateColorInPoint(int x, int y) 方法不会引发 NewColor 属性的 OnPropertyChanged(),因此 XAML 永远不会知道 NewColor 已更改。 You have to change NewColor directly and not by its backing field like this NewColor = _paletteColors[x, y];你必须直接改变 NewColor 而不是像这样NewColor = _paletteColors[x, y]; . .

give me to know if i was helped.让我知道我是否得到了帮助。 regards,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM