简体   繁体   English

SolidColorBrush颜色绑定问题

[英]SolidColorBrush Color binding issues

So i'm having some binding issues ATM with my "GroupColour" property on my ObjA class. 因此,我的ObjA类的“ GroupColour”属性遇到一些绑定问题,导致ATM出现问题。 I also have a CurrentState variable, which, based on its value, returns a Brush (this is done in the converter below). 我还有一个CurrentState变量,该变量根据其值返回一个Brush(在下面的转换器中完成)。

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace UIChemicalMelt
{
    public class StateToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch (value)
            {
                case State.INACTIVE:
                    return Utilities.ResourceDictionary["HoverOverColourInactive"] as Brush;
                case State.ACTIVE:
                    return Utilities.ResourceDictionary["HoverOverColourActive"] as Brush;
                case State.ACTIVE_GROUP_SET:
                    return Utilities.ResourceDictionary["BackgroundBasedOnGroupColour"] as Brush;
                case State.HIGHLIGHTED:
                    return Utilities.ResourceDictionary["HighlightThemeColour"] as Brush;
                case State.PROCESSING:
                    return Utilities.ResourceDictionary["ProcessingWellColour"] as Brush;
                default:
                    return Brushes.Transparent.Color;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

It is used in my ControlTemplate Triggers of my buttons that I create in my View XAML 在View XAML中创建的按钮的ControlTemplate触发器中使用了它

                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter Property="Background" Value="{Binding CurrentState, Converter={StaticResource StateToColorConverter}}"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Background" Value="{Binding CurrentState, Converter={StaticResource StateToColorConverter}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>

It all seems to work fine, but I get a XAML error... 似乎一切正常,但出现XAML错误...

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path=GroupColour; BindingExpression:路径= GroupColour; DataItem=null; 的DataItem = NULL; target element is 'SolidColorBrush' (HashCode=46663997); 目标元素是'SolidColorBrush'(HashCode = 46663997); target property is 'Color' (type 'Color') 目标属性为“颜色”(类型为“颜色”)

I don't know why tbh.. 我不知道为什么

Sorry, my answer can not solove your problem System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GroupColour; DataItem=null; target element is 'SolidColorBrush' (HashCode=46663997); target property is 'Color' (type 'Color') 抱歉,我的回答不能解决您的问题System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GroupColour; DataItem=null; target element is 'SolidColorBrush' (HashCode=46663997); target property is 'Color' (type 'Color') System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GroupColour; DataItem=null; target element is 'SolidColorBrush' (HashCode=46663997); target property is 'Color' (type 'Color') System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GroupColour; DataItem=null; target element is 'SolidColorBrush' (HashCode=46663997); target property is 'Color' (type 'Color') and I think your code will not send this message and I need more code. System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GroupColour; DataItem=null; target element is 'SolidColorBrush' (HashCode=46663997); target property is 'Color' (type 'Color') ,我认为您的代码将不会发送此消息,因此我需要更多代码。

But your converter return a Color in this code return Brushes.Transparent.Color . 但是您的转换器在此代码中return Brushes.Transparent.Color Colorreturn Brushes.Transparent.Color And it will set the color to background that the backgroud value type is brush. 并且它将backgroud值类型为brush的颜色设置为背景。

It also will send the bind message when you open the debug output window. 当您打开调试输出窗口时,它还将发送绑定消息。

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentState; DataItem=null; target element is 'Grid' (Name=''); target property is 'Background' (type 'Brush')

How to fix it? 如何解决? Try to replace return Brushes.Transparent.Color to Brushes.Transparent and you can see this code. 尝试将return Brushes.Transparent.Color替换为Brushes.Transparent ,您可以看到此代码。

            switch (value)
            {
                case State.INACTIVE:
                    return Utilities.ResourceDictionary["HoverOverColourInactive"] as Brush;
                case State.ACTIVE:
                    return Utilities.ResourceDictionary["HoverOverColourActive"] as Brush;
                case State.ACTIVE_GROUP_SET:
                    return Utilities.ResourceDictionary["BackgroundBasedOnGroupColour"] as Brush;
                case State.HIGHLIGHTED:
                    return Utilities.ResourceDictionary["HighlightThemeColour"] as Brush;
                case State.PROCESSING:
                    return Utilities.ResourceDictionary["ProcessingWellColour"] as Brush;
                default:
-                    return Brushes.Transparent.Color;
+                    return Brushes.Transparent;
            }

When you simply choose a color in xaml WPF will automatically run your color through an implicit converter to wrap it in a brush. 当您简单地在xaml中选择一种颜色时,WPF将自动通过一个隐式转换器运行您的颜色,以将其包裹在画笔中。 Since you're supplying your own converter you will no longer be able to benefit from the implicit WPF one and thus you'll want to return the brush yourself. 由于您要提供自己的转换器,因此您将不再能够从隐式WPF中受益,因此您需要自己归还电刷。

Heres your sample with just 'new SolidColorBrush' added to the various lines to satisfy the Background binding. 在这里,您的示例仅在各个行中添加了“新SolidColorBrush”,以满足Background绑定。

public class StateToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (value)
        {
            case State.INACTIVE:
                return new SolidColorBrush(Utilities.ResourceDictionary["HoverOverColourInactive"]);
            case State.ACTIVE:
                return new SolidColorBrush(Utilities.ResourceDictionary["HoverOverColourActive"]);
            case State.ACTIVE_GROUP_SET:
                return new SolidColorBrush(Utilities.ResourceDictionary["BackgroundBasedOnGroupColour"]);
            case State.HIGHLIGHTED:
                return new SolidColorBrush(Utilities.ResourceDictionary["HighlightThemeColour"]);
            case State.PROCESSING:
                return new SolidColorBrush(Utilities.ResourceDictionary["ProcessingWellColour"]);
            default:
                return Brushes.Transparent;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

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

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