WPF自定义按钮和绑定

时间:2013-11-28 16:34:29

标签: wpf xaml

我有一个WPF应用程序。我创建了一个自定义样式的按钮。该按钮包含一个文本框&两个图像。我在App.xaml文件中创建了该样式。我的问题是这个。我想在MainWindow.xaml文件中创建一些按钮,这些按钮将使用我的App.xaml中的样式。这有效,但现在我想绑定图像&文本框到名为Securities的列表对象中的元素。

因此,当我在MainWindow中创建一个按钮时,如何设置文本框和文本框的绑定?图象?

 <!-- style for button -->
    <Style x:Key="buttSecurity" TargetType="Button">
        <Setter Property="Margin" Value="1,1,1,1"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">                    
                    <Grid Background="{StaticResource brushSecurityButtRadial}">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource txtSecurity}"/>
                        <Image Grid.Row="1" Grid.Column="0"/>
                        <Image Grid.Row="1" Grid.Column="1"/>
                    </Grid>               
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  

2 个答案:

答案 0 :(得分:0)

尝试使用TemplateBinding

    <Window.Resources>
    <Style x:Key="buttSecurity" TargetType="Button">
        <Setter Property="Margin" Value="1,1,1,1"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="Red">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        **<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>**
                        <Image Grid.Row="1" Grid.Column="0"/>
                        <Image Grid.Row="1" Grid.Column="1"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Content="{Binding MyText}" Style="{StaticResource buttSecurity}"/>

答案 1 :(得分:0)

你无法直接做你想做的事。实现该功能的方法是将ControlTemplateStyle放入UserControl,然后添加三个DependencyProperty以绑定到:

    <Style x:Key="buttSecurity" TargetType="Button">
        <Setter Property="Margin" Value="1,1,1,1"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">                    
                    <Grid Background="{StaticResource brushSecurityButtRadial}">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Style=
"{StaticResource txtSecurity}" Text="{Binding TextValue, RelativeSource={RelativeSource 
AncestorType={x:Type YourXmlNamespacePrefix:YourButtonUserControl}}}" />
                        <Image Grid.Row="1" Grid.Column="0" Source="{Binding 
ImageSource1, RelativeSource={RelativeSource AncestorType={x:Type 
YourXmlNamespacePrefix:YourButtonUserControl}}}" />
                        <Image Grid.Row="1" Grid.Column="1" Source="{Binding 
ImageSource2, RelativeSource={RelativeSource AncestorType={x:Type 
YourXmlNamespacePrefix:YourButtonUserControl}}}" />
                    </Grid>               
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style> 

假设您有一个Images文件夹,那么您可以像这样使用UserControl

<YourXmlNamespacePrefix:YourButtonUserControl TextValue="Click me" 
    ImageSource1="/AppName;component/Images/Image1.jpg" 
    ImageSource1="/AppName;component/Images/Image2.jpg" />

我不打算向您展示如何声明DependencyProperty,因为有大量资源可以在网上找到。