UserControl后台属性刷子绑定不起作用

时间:2018-01-04 14:56:27

标签: c# wpf xaml user-controls

您好我正在尝试使用在viewmodel中绑定的bacground初始化usercontrol。在模型中设置颜色,但依赖属性不接受它。

HeaderCropper xaml:

<UserControl x:Class="x.CustomControls.x"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:converters="clr-x.Converters"
         mc:Ignorable="d" 
         d:DesignHeight="300"
         d:DesignWidth="300"

         Width="{Binding CropperWidth,ElementName=HeaderCropper}"
         Height="{Binding CropperHeight,ElementName=HeaderCropper}"
         x:Name="HeaderCropper">

<Thumb x:Name="CroppableArea" BorderThickness="1" Grid.Column="0" Grid.Row="0" Background='{Binding CropperColor}' BorderBrush="Black" Opacity="0.2"  

HeaderCropper类:

public static DependencyProperty ColorProperty;

static HeaderCroper()
{
ColorProperty = DependencyProperty.Register("CropperColor",
                                                typeof(Brush), typeof(HeaderCroper), new FrameworkPropertyMetadata(null));
}

public Brush CropperColor
    {
        get { return (Brush)GetValue(ColorProperty); }
        set
        {
            SetValue(ColorProperty, value);
        }
    }

HeaderCropper绑定:

<DataTemplate>
                    <custom:HeaderCroper Panel.ZIndex="1" Width="50" Height="50" CropperHeight="{Binding Height,Mode=TwoWay,NotifyOnTargetUpdated=True}" 
                                             CropperWidth="{Binding Width,Mode=TwoWay,NotifyOnTargetUpdated=True}" 
                                             MaxCropperHeight="{Binding ElementName=Image,Path=RenderSize.Height}" 
                                             MaxCropperWidth="{Binding ElementName=Image,Path=RenderSize.Width}" 
                                             InfoCommand="{Binding InfoCommand}"
                                             InfoCommandParameter="{Binding Id}"
                                             DeleteCropperCommand="{Binding DeleteCommand}"
                                             DeleteCropperCommandParameter="{Binding Id}"
                                             CropperLeft="{Binding CropperLeft,Mode=TwoWay,NotifyOnTargetUpdated=True}"
                                             CropperTop="{Binding CropperTop,Mode=TwoWay,NotifyOnTargetUpdated=True}"
                                             MainCanvas ="{Binding ElementName=CropperCanvas,NotifyOnTargetUpdated=True}"
                                             Canvas.Left="0"
                                             CropperColor="{Binding CropperColor,Mode=TwoWay,NotifyOnTargetUpdated=True}"
                                             />
                </DataTemplate>

正如您所见,传递了CropperColor参数,但是标题裁剪器忽略它并且没有设置颜色。有什么想法吗?

更新: 当我按私人方法按键调试时,我看到颜色已设置但在usercontroll初始化时为null。

0 个答案:

没有答案