如何通过DependencyProperty将属性传递给WPF样式?

时间:2019-01-31 08:26:32

标签: wpf vb.net mvvm dynamic dependency-properties

我正在尝试为使用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 :(得分:0)

例如:-

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}"/>

现在,无论何时更改用户名,它都会反映到UI !!!