从Color属性的绑定不适用于模板XAML WPF中的SelectedColor

时间:2019-03-07 12:11:04

标签: c# wpf xaml binding telerik

我将此模板作为OpacitySlider.xaml文件的一部分。

在DropDownContent内,我有RadColorSelector,并且已经在该模板中添加了选择器Control of OpacitySlider旁边。

OpacitySlider具有 Color 的依赖项属性,当我选择颜色和不透明度时会更新该属性,但是RadColorSelector的 SelectedColor 似乎无法获得该值(我想)。

我需要帮助。

OpacitySlider.xaml中的模板零件

<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type telerik:RadColorPicker}">
                <Grid x:Name="Root"
                      HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                      Height="{TemplateBinding Height}"
                      VerticalAlignment="{TemplateBinding VerticalAlignment}"
                      Width="{TemplateBinding Width}">
                    <telerik:RadSplitButton x:Name="SplitButton"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            ButtonPartStyle="{TemplateBinding ColorButtonStyle}"
                                            BorderThickness="{TemplateBinding BorderThickness}"
                                            Background="{TemplateBinding Background}"
                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                            Content="{TemplateBinding Content}"
                                            DropDownIndicatorVisibility="Visible"
                                            DropDownPlacement="{Binding DropDownPlacement, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                            IsChecked="{Binding IsOpen, RelativeSource={RelativeSource Self}}"
                                            InnerCornerRadius="0"
                                            Padding="{TemplateBinding Padding}"
                                            Style="{TemplateBinding SplitButtonStyle}"
                                            TogglePartStyle="{TemplateBinding ExpanderButtonStyle}"
                                            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                        <telerik:RadSplitButton.DropDownContent>
                            <Grid>
                                <Grid.Resources>
                                    <converters:OpacityConverter x:Key="OpacityConverter" />
                                </Grid.Resources>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <telerik:RadColorSelector x:Name="ColorSelector"
                                                          AdditionalContentTemplate="{TemplateBinding AdditionalContentTemplate}"
                                                          AutomaticColor="{Binding AutomaticColor, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                          AdditionalContent="{TemplateBinding AdditionalContent}"
                                                          BorderBrush="{TemplateBinding BorderBrush}"
                                                          BorderThickness="1"
                                                          Background="{TemplateBinding Background}"
                                                          CommandTarget="{TemplateBinding CommandTarget}"
                                                          ColorPropertyPath="{TemplateBinding ColorPropertyPath}"
                                                          CommandParameter="{TemplateBinding CommandParameter}"
                                                          Command="{TemplateBinding Command}"
                                                          Foreground="{TemplateBinding Foreground}"
                                                          FontWeight="{TemplateBinding FontWeight}"
                                                          FontStyle="{TemplateBinding FontStyle}"
                                                          HeaderPaletteVisibility="{TemplateBinding HeaderPaletteVisibility}"
                                                          HeaderPalette="{TemplateBinding HeaderPalette}"
                                                          HeaderPaletteItemsSource="{TemplateBinding HeaderPaletteItemsSource}"
                                                          IsRecentColorsActive="{TemplateBinding IsRecentColorsActive}"
                                                          Margin="-1"
                                                          MainPaletteItemsStyle="{TemplateBinding MainPaletteItemsStyle}"
                                                          MainPaletteHeaderText="{TemplateBinding MainPaletteHeaderText}"
                                                          MainPaletteOrientation="{TemplateBinding MainPaletteOrientation}"
                                                          MainPaletteColumnsCount="{TemplateBinding MainPaletteColumnsCount}"
                                                          MainPalette="{TemplateBinding MainPalette}"
                                                          MainPaletteItemsSource="{TemplateBinding MainPaletteItemsSource}"
                                                          NoColorVisibility="{TemplateBinding NoColorVisibility}"
                                                          NoColorText="{TemplateBinding NoColorText}"
                                                          PaletteItemsTemplate="{TemplateBinding PaletteItemsTemplate}"
                                                          RecentColorsItemsSource="{TemplateBinding RecentColorsItemsSource}"
                                                          RecentColorsHeaderText="{TemplateBinding RecentColorsHeaderText}"
                                                          RecentColorsVisibility="{TemplateBinding RecentColorsVisibility}"
                                                          StandardPaletteItemsSource="{TemplateBinding StandardPaletteItemsSource}"
                                                          Style="{TemplateBinding ColorSelectorStyle}"
                                                          StandardPaletteVisibility="{TemplateBinding StandardPaletteVisibility}"
                                                          SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                          SelectedColor="{Binding SelectedColor, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                          StandardPaletteHeaderText="{TemplateBinding StandardPaletteHeaderText}"
                                                          StandardPalette="{TemplateBinding StandardPalette}" />
                                <Grid Grid.Row="0"
                                      Grid.Column="1"
                                      Grid.RowSpan="2">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Label Content="Opacity"
                                           FontSize="12"
                                           FontWeight="Normal"
                                           Margin="0,0,0,5"
                                           Grid.Row="0" />
                                    <TextBox Grid.Row="1"
                                             Width="40"
                                             FontSize="11"
                                             Text="{Binding Path=Value, ElementName=TransparencySlider, Mode=Default,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource OpacityConverter}}" />
                                    <controls:OpacitySlider Name="TransparencySlider"
                                                            Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedColor, Mode=TwoWay}"
                                                            Grid.Row="2"
                                                            Margin="0,10,0,10"
                                                            HorizontalAlignment="Center"
                                                            HorizontalContentAlignment="Center"
                                                            RenderTransformOrigin="0,0">
                                        <Slider.LayoutTransform>
                                            <RotateTransform CenterX="0"
                                                             CenterY="0"
                                                             Angle="-90" />
                                        </Slider.LayoutTransform>
                                    </controls:OpacitySlider>
                                </Grid>
                            </Grid>
                        </telerik:RadSplitButton.DropDownContent>
                    </telerik:RadSplitButton>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

OpacitySlider.cs

    public class OpacitySlider : Slider
{
    #region private members
    private bool _isOpacityChanging;
    private bool _isColorChanging;
    private RadColorSelector _colorSelector;
    #endregion

    static OpacitySlider()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(OpacitySlider), new FrameworkPropertyMetadata(typeof(Slider)));
    }

    public OpacitySlider()
    {
        Minimum = 0;
        Maximum = 255;
    }

    public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(OpacitySlider), new PropertyMetadata(default(Color), OnBaseColorPropertyChanged));

    private static void OnBaseColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as OpacitySlider).OnBaseColorChanged();
    }

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

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        _colorSelector = (RadColorSelector)Template.FindName("ColorSelector", this);
        _colorSelector.SelectedColorChanged += _colorSelector_SelectedColorChanged;
    }

    private void _colorSelector_SelectedColorChanged(object sender, System.EventArgs e)
    {
    }

    private void OnBaseColorChanged()
    {
        if (_isColorChanging)
        {
            return;
        }

        _isColorChanging = true;
        try
        {
            Value = Color.A;
        }
        finally
        {
            _isColorChanging = false;
        }
    }

    protected override void OnValueChanged(double oldValue, double newValue)
    {
        if (_isOpacityChanging)
        {
            return;
        }

        _isOpacityChanging = true;
        try
        {
            base.OnValueChanged(oldValue, newValue);
            Color = Color.FromArgb((byte)Value, Color.R, Color.G, Color.B);
            if (_colorSelector != null)
            {
                _colorSelector.SelectedColor = Color;
            }

        }
        finally
        {
            _isOpacityChanging = false;
        }
    }
}

此外,在该文件OpacitySlider.cs中,我有 OnApplyTemplate(),其中我尝试通过代码将其获取到 RadColorSelector ,但我没有输入NULL _colorSelector属性中。

我该如何解决?

谢谢!

0 个答案:

没有答案