Как я могу передать свойства в стиль WPF с DependencyProperty? - PullRequest
0 голосов
/ 31 января 2019

Я пытаюсь создать повторно используемый стиль для кнопки с WPF, и я хочу передать параметры из .xaml, но это не работает, даже когда я вставляю точку останова в класс ButtonStyle во время отладки, он не вводится в него, япропустить что-то, и я не нашел это.

Спасибо.

ButtonStyle Класс:

Public Class ButtonStyle
Inherits System.Windows.Controls.Button

Public Sub New()
End Sub

Public Shared ReadOnly BackgroundColorProperty As DependencyProperty = 
DependencyProperty.Register("BackgroundColor", GetType(Brush), 
GetType(ButtonStyle))

Public Property BackgroundColor As Brush
    Get
        Return CType(GetValue(BackgroundColorProperty), Brush)
    End Get
    Set(value As Brush)
        SetValue(BackgroundColorProperty, value)
    End Set
End Property

Public Shared ReadOnly BackgroundColorHoverProperty As DependencyProperty 
= DependencyProperty.Register("BackgroundColorHover", GetType(Brush), 
GetType(ButtonStyle))

Public Property BackgroundColorHover As Brush
    Get
        Return CType(GetValue(BackgroundColorHoverProperty), Brush)
    End Get
    Set(value As Brush)
        SetValue(BackgroundColorHoverProperty, value)
    End Set
End Property

Public Shared ReadOnly BorderColorProperty As DependencyProperty = 
DependencyProperty.Register("BorderColor", GetType(Brush), 
GetType(ButtonStyle))

Public Property BorderColor As Brush
    Get
        Return CType(GetValue(BorderColorProperty), Brush)
    End Get
    Set(value As Brush)
        SetValue(BorderColorProperty, value)
    End Set
End Property

Public Shared ReadOnly BorderColorHoverProperty As DependencyProperty = 
DependencyProperty.Register("BorderColorHover", GetType(Brush), 
GetType(ButtonStyle))

Public Property BorderColorHover As Brush
    Get
        Return CType(GetValue(BorderColorHoverProperty), Brush)
    End Get
    Set(value As Brush)
        SetValue(BorderColorHoverProperty, value)
    End Set
End Property

Public Shared ReadOnly IconeProperty As DependencyProperty = 
DependencyProperty.Register("Icone", GetType(ImageSource), 
GetType(ButtonStyle))

Public Property Icone As ImageSource
    Get
        Return CType(GetValue(IconeProperty), ImageSource)
    End Get
    Set(value As ImageSource)
        SetValue(IconeProperty, value)
    End Set
End Property End Class

MainWindow.xaml:

 <Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="DictionaryResources.xaml"/>
        </ResourceDictionary.MergedDictionaries >
    </ResourceDictionary>
</Window.Resources>

<Grid>
    <local:ButtonStyle Width="150" Height="50" Style="{StaticResource 
StyleBoutonHello}"   Icone="img.png" BorderColor="red" 
BackgroundColor="red" BackgroundColorHover="Blue" BorderColorHover="blue" 
Content="Hello"></local:ButtonStyle>
</Grid>

DictionnaryResource.xaml:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">

<Style x:Name="StyleBoutonHello" x:Key="StyleBoutonHello" TargetType=" 
{x:Type local:ButtonStyle}">
    <Setter Property="BorderBrush"  Value="{Binding BorderColor}" />
    <Setter Property="Background"  Value="red" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="FontWeight" Value="Medium" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Content" Value="Hello" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Image Name="Img" Width="30" Height="30" Source="{Binding 
Icone, RelativeSource={RelativeSource TemplatedParent}}" Stretch="None" 
HorizontalAlignment="Center" VerticalAlignment="Center"/>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" TargetName="Img"  
Value="{Binding Icone, RelativeSource={RelativeSource TemplatedParent}}" 
/>
                        <Setter Property="Background" Value="{Binding 
BackgroundColorHover}" />
                    </Trigger>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>  
</ResourceDictionary>

1 Ответ

0 голосов
/ 31 января 2019

Например: -

1) Реализовать интерфейс INotifyPropertyChanged.

Class MainWindow
 Implements INotifyPropertyChanged

 Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
 Private Sub OnPropertyChanged(ByVal name As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
 End Sub

 Public Shared ReadOnly MyProperty1 As DependencyProperty = DependencyProperty.Register("UserName", GetType(String), GetType(MainWindow), New UIPropertyMetadata(String.Empty))

 Public Property UserName() As String
    Get
        Return DirectCast(GetValue(MyProperty1), String)
    End Get
    Set
        SetValue(MyProperty1, Value)
        OnPropertyChanged("Name")
    End Set
 End Property  
End Class

2) Теперь привязать значение в XAML

 <TextBlock Text="{Binding UserName,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>

Теперь при каждом изменении имени пользователяэто отразится на интерфейсе !!!.

...