【问题标题】:Using converter to change backgroundcolor of usercontrol使用转换器更改用户控件的背景颜色
【发布时间】:2013-09-13 08:40:07
【问题描述】:

我有一个用户控件,我想动态更改此控件的背景颜色。

当视图模型中设置了某个枚举时,我希望颜色发生变化,因此我使用转换器将枚举转换为 SolidColorBrush。

当我将此转换器放在另一个控件中时,它工作正常。但是当我将它放在 xaml 文件顶部的用户控件属性中时,它会出错。

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ee="http://schemas.microsoft.com/expression/2010/effects"
mc:Ignorable="d"
xmlns:converters="clr-namespace:UserControlSolution.Converter"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50" 
VerticalAlignment="Top"
Background="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"
Margin="2,0,0,5"
>

<UserControl.Resources>
    <converters:CallStatusEnumToBackgroundColor x:Key="CallStatusBackgroundConverter"/>
    <converters:StatusEnumToStatusResourceConverter x:Key="StatusIconConverter"/>
    <BooleanToVisibilityConverter x:Key="BoolToVis" />

    <Style x:Key="SelectedStyle" TargetType="{x:Type WrapPanel}">
        <Setter Property="Background" Value="Transparent"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="true">
                <Setter Property="Background" Value="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"/>
                <Setter Property="Opacity" Value=".92"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" VerticalAlignment="Top" HorizontalAlignment="Stretch">        
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="50"/>
    </Grid.ColumnDefinitions>
 </Grid>

我的转换器只是从枚举转换为颜色

namespace UserControlSolution.Converter
{
    [ValueConversion(typeof(CallEnum), typeof(SolidColorBrush))]
    public class CallStatusEnumToBackgroundColor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((CallEnum)value)
            {
                case CallEnum.Connected:
                    return (SolidColorBrush)Application.Current.Resources["Red"];
                case CallEnum.ConnectedIntern:
                    return (SolidColorBrush)Application.Current.Resources["Blue"];
                case CallEnum.Hold:
                    return (SolidColorBrush)Application.Current.Resources["Orange"];
                case CallEnum.Idle:
                    return (SolidColorBrush)Application.Current.Resources["DarkGrey"];
                case CallEnum.OffRing:
                    return (SolidColorBrush)Application.Current.Resources["Yellow"];
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

【问题讨论】:

  • Xaml 中背景行上的 XamlParseException。
  • 什么是内部Exception???
  • 我认为它不像他上面实现的那样工作,但它是这样工作的:&lt;local:myUserControl Background="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"/&gt;
  • 很好看的@FlorianGl...你能把它放到一个答案中,以便这个问题得到真正的回答吗?
  • 仅供参考,如果你返回带有颜色名称的字符串,它也可以工作。

标签: c# wpf user-controls


【解决方案1】:

将其放在资源之后并从用户控件中删除绑定:

<UserControl.Background>
   <SolidColorBrush Color="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"/>
</UserControl.Background>

【讨论】:

  • 转换器返回 SolidColorBrush 而不是 Color
  • 我需要更改哪些内容才能使用 SolidColorBrush?
  • 正是我的想法。
  • 你调整你的转换器了吗?所以它返回一个Color,而不是SolidColorBrush
  • 你调试你的转换器类了吗?
【解决方案2】:

严格来说,接受的答案不是@robby-smet 的原始问题(和 cmets)的答案,即如何绑定到 SolidColorBrush。正确完成此操作的方式与@aSterX 的答案中所建议的不同,而是

<UserControl.Background>
  <SolidColorBrush>
    <Binding Path="CallStatus"
             Converter="{StaticResource CallStatusBackgroundConverter}}" />
  </SolidColorBrush>
</UserControl.Background>

【讨论】:

    【解决方案3】:

    无论错误是什么(甚至可能是错误),我想指出您确实应该检查任何Converter 类中的输入类型:

    if (value == null || value.GetType() != typeof(YourRequiredType)) 
        return DependencyProperty.UnsetValue;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      相关资源
      最近更新 更多