Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 边框背景绑定只工作一次_C#_Wpf_Mvvm - Fatal编程技术网

C# 边框背景绑定只工作一次

C# 边框背景绑定只工作一次,c#,wpf,mvvm,C#,Wpf,Mvvm,我的ViewModel中有一个与颜色属性绑定的背景边框。但背景颜色在初始化后仅更改一次。同时,我将3个文本框绑定到相同的属性(R、G和B),它们工作正常 为什么文本框工作正常而边框不正常?这是border的代码: <Border x:Name="bNewColor" BorderBrush="Black" > <Border.Background> <SolidColorBrush Color="{Binding NewColor}"/>

我的ViewModel中有一个与颜色属性绑定的背景边框。但背景颜色在初始化后仅更改一次。同时,我将3个文本框绑定到相同的属性(R、G和B),它们工作正常

为什么文本框工作正常而边框不正常?这是border的代码:

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

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];
    }

...
}

正如我所看到的,您的determineColorinpoint(intx,inty)方法没有提高 NewColor属性的OnPropertyChanged(),因此XAML永远不会知道NewColor已更改。您必须直接更改NewColor,而不是像这样通过其支持字段
NewColor=\u paletteColors[x,y]

让我知道我是否得到了帮助。
在这方面,

如果不看到代码可靠地再现了问题,就无法解释代码为什么不能工作。同时,查看调试输出…绑定问题通常是由绑定过程中的实际错误引起的,这些错误将显示在程序的调试输出中。我看不出如何更改
NewColor
,很可能您正试图修改文本框中的R、G、B组件,而这些组件不会以绑定它们的方式工作。不,我不直接修改文本框。它们只改变值,然后改变新颜色
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();
    }

...
}
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];
    }

...
}