2010-04-07 176 views
2

我有WPF窗体,其中有许多按钮具有相同的代码。所有按钮的外观必须是相同的 例如,代码为这些按钮WPF按钮样式

<Button x:Name="btnAddRelative" Width="120" Click="btnAddRelative_Click" > 
    <Button.Content> 
     <StackPanel Orientation="Horizontal"> 
      <Image Height="26" HorizontalAlignment="Left"> 
        <Image.Source> 
         <BitmapImage UriSource="images/add.png" /> 
        </Image.Source> 
      </Image> 
      <TextBlock Text=" Add Relative" Height="20" VerticalAlignment="Center"/> 
     </StackPanel> 
    </Button.Content> 
</Button> 

如何创建一种风格,并用它为我所有的按钮之一。所有的按钮都有相同的PNG图像,只有它们的文字不同。我怎样才能做到这一点。 我试图在资源与节Style对象做到这一点:

<UserControl.Resources> 
    <Style TargetType="Button" x:Key="AddStyle"> 
     <Setter Property="Content"> 
      <Setter.Value> 
       <StackPanel Orientation="Horizontal"> 
        <Image Height="26" HorizontalAlignment="Left"> 
         <Image.Source> 
          <BitmapImage UriSource="images/add.png" /> 
         </Image.Source> 
        </Image> 
        <TextBlock Text=" " Height="20" VerticalAlignment="Center"/> 
       </StackPanel> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

但这码不起作用。任何人都可以知道我该怎么做?

回答

7

显示其它任何事情如果图像修复你可以在它硬编码款式,并使用内容财产按钮 bin到内容Tex tBox

<Style x:Key="ButtonStyle" TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border 
          Background="{TemplateBinding Background}"        
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}"> 
          <StackPanel 
           Orientation="Horizontal"> 
           <!--<Image Height="26" HorizontalAlignment="Left"> 
            <Image.Source> 
             <BitmapImage UriSource="images/add.png" /> 
            </Image.Source> 
           </Image>--> 
           <TextBlock 
            Foreground="{TemplateBinding Foreground}" 
            Text="{TemplateBinding Content}" 
            Height="20" 
            VerticalAlignment="{TemplateBinding VerticalAlignment}"/> 
          </StackPanel> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
+0

非常感谢。伟大的员工。它工作,因为我想!!!! – Polaris 2010-04-07 12:43:49

0

试着改变你的风格如下

<UserControl.Resources> 
     <Style 
      TargetType="Button" 
      x:Key="AddStyle"> 
      <Setter 
       Property="Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <StackPanel 
          Orientation="Horizontal"> 
          <Image 
           Height="26" 
           HorizontalAlignment="Left"> 
           <Image.Source> 
            <BitmapImage 
             UriSource="images/add.png" /> 
           </Image.Source> 
          </Image> 
          <TextBlock 
           Text=" " 
           Height="20" 
           VerticalAlignment="Center" /> 
         </StackPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

[编辑被评论的启发]

您可以创建一个新的用户控件,让我们把它叫做AddButtonContent包含您的StackPanel和这样的,然后包括在您的按钮内,如下所示:

<Button> 
    <local:AddButtonContent 
     ButtonText="Testing, one, two, three" /> 
</Button> 

你需要添加一个叫做local的xmlns引用(或者你想调用它的任何东西)到所有的按钮上。

AddButtonContent UserControl的代码隐藏部分将需要下面的代码,并且您需要命名您的TextBlock(我在此示例中使用了testText)。

公共静态的DependencyProperty ButtonTextProperty = DependencyProperty.Register( “ButtonText”, typeof运算(字符串), typeof运算(AddButtonContent) 新PropertyMetadata( “”,onTextChangedCallback));

public string ButtonText 
{ 
    get { return (string)GetValue(ButtonTextProperty); } 
    set 
    { 
     SetValue(ButtonTextProperty, value); 
    } 
} 


static void onTextChangedCallback(
    DependencyObject dobj, 
    DependencyPropertyChangedEventArgs args) 
{ 
    AddButtonContent abc = dobj as AddButtonContent; 
    abc.testText.Text = args.NewValue.ToString(); 
} 
+0

它的工作原理。但不是我想要的。我在ResourceDictionary中有应用程序级别样式。你的风格取代我的风格。主要的事情我想显示我的PNG和特定的文本,但不清理我的主要按钮样式 – Polaris 2010-04-07 12:01:31

+0

Polaris,使用UserControl并将按钮的内容设置为这个新的控件可能适合你。更新了我的答案。 – 2010-04-07 12:30:32

+0

在一个stackPanel中我有TextBlock。 TextBlock Text必须是每个按钮的特定。使用userControl版本我不能更改TextBlock文本。 – Polaris 2010-04-07 12:30:59

2

只是试试这个

<Window.Resources> 
    <Style TargetType="Button" 
      x:Key="AddStyle"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <StackPanel Orientation="Horizontal"> 
         <Image Height="26" 
           Width="20" 
           HorizontalAlignment="Left"> 
          <Image.Source> 
           <BitmapImage UriSource="/WpfApplication33;component/Images/MoveLeft.png" /> 
          </Image.Source> 
         </Image> 
         <TextBlock Text ="{TemplateBinding Content}" 
            Height="20" 
            /> 
        </StackPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</Window.Resources> 
<Grid> 
    <StackPanel> 
    <Button Style="{StaticResource AddStyle}" 
      Height="25" Width="100" 
      Content="Button1"></Button> 
    <Button Style="{StaticResource AddStyle}" 
      Height="25" 
      Width="100" 

      Content="Button22"></Button> 
     <Button Style="{StaticResource AddStyle}" 
       Height="25" 
       Width="100" 
       Content="Button2233"></Button> 
     <Button Style="{StaticResource AddStyle}" 
       Height="25" 
       Width="100" 
       Content="Button2332"></Button> 

    </StackPanel> 
</Grid> 

注意:使用ContentPresenter,而不是TextBlock的,如果你有比纯文本

+0

大声笑,相同的代码,但你花较少的时间写下来:D – Drake 2010-04-07 12:31:31