2010-04-07 120 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