DependencyProperty как привязать его к сеттеру

Рейтинг: 0Ответов: 1Опубликовано: 08.08.2023
 public class Cust: ComboBox
{
 #region CornerRadius у combobox и выпадающего списка
            public static readonly DependencyProperty CornerRadius_CustomComboBox_Property = DependencyProperty.RegisterAttached(
    nameof(CornerRadius_CustomComboBox), typeof(CornerRadius), typeof(CustomComboBox), new FrameworkPropertyMetadata(new CornerRadius(6.0)));
    
            public static void SetCornerRadius_Cust(UIElement element, CornerRadius value) => element.SetValue(CornerRadius_CustomComboBox_Property, value);
    
            public static CornerRadius GetCornerRadius_Cust(UIElement element) => (CornerRadius)element.GetValue(CornerRadius_CustomComboBox_Property);
            public CornerRadius CornerRadius_CustomComboBox
            {
                get { return (CornerRadius)GetValue(CornerRadius_CustomComboBox_Property); }
                set { SetValue(CornerRadius_CustomComboBox_Property, value); }
            }
            #endregion
}

wpf

Window.Resources>

        <Style x:Key="Test" BasedOn="{StaticResource ComboBoxStyle}" TargetType="{x:Type local:Cust}">
            <Setter Property="Background_Cust" Value="Violet"></Setter>
            <Setter Property="BorderBrush_Cust" Value="Red"></Setter>
            <Setter Property="CornerRadius_Cust" Value="10 1 1 1"></Setter>

        </Style>
    </Window.Resources>
    <Grid>
        <local:Cust Style="{StaticResource Test}" Margin="0,0,0,325">
            <TextBlock>Text</TextBlock>
            <TextBlock>Text</TextBlock>
            <TextBlock>Text</TextBlock>
            <TextBlock>Text</TextBlock>
             <TextBlock>Text</TextBlock>
        </local:Cust>
    </Grid>

Я хочу использовать CornerRadius_CustomComboBox в сеттере, но в ошибках пишется Свойство "CornerRadius_Cust" не является свойством DependencyProperty. Для использования в разметке не присоединенные свойства необходимо предоставить целевому типу с доступным свойством экземпляра "CornerRadius_Cust". Для присоединенных свойств в объявляющем типе должны быть представлены статические методы "GetCornerRadius_Cust" и "SetCornerRadius_Cust". Хотя при запуске углы закругляются у элемента.

Ответы

▲ 0

У меня есть свойство DPс именем CornerRadius_Cust Сейчас моей самой большой ошибкой было то что в именование DependencyProperty я нарушил правило именования. Нужно к имени строго прописывать Property без лишних букв или символов. Для сравнения: Оригинальное имя CornerRadius_Cust Имя для DependencyProperty: CornerRadius_CustProperty Если указываете Get или Set то после этого префикса, также должно идти имя которое вашего свойства. Сам код для примера.

 public static readonly DependencyProperty CornerRadius_CustProperty = DependencyProperty.Register(
nameof(CornerRadius_Cust), typeof(CornerRadius), typeof(Cust), new PropertyMetadata(new CornerRadius(6.0)));

        public CornerRadius CornerRadius_Cust
        {
            get { return (CornerRadius)GetValue(CornerRadius_CustProperty); }
            set { SetValue(CornerRadius_CustProperty, value); }
        }