2013-11-28 161 views
0

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

因此,当我在我的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> 
+0

http://stackoverflow.com/questions/6807006/c-sharp-databinding-tutorial – Naresh

+1

当你有答案时,你不会再改变或删除你的问题吗? – Sheridan

+0

我没有改变我的最后一个问题。我明白你的观点,我把错误的答案归功于它,并且从此更新了它。我从来没有删除我的问题不知道你从哪里得到的? – mHelpMe

回答

0

你不能这样做,你直接想要的东西。实现这一功能的方法是把你的ControlTemplateStyleUserControl,然后添加三个DependencyProperty s到绑定到:

<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,因为有对资源的负荷在网上找到。

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