2017-01-11 14 views
0

XAMLにボタンがあり、私が必要としていたスタイルになっています。これは、そのボタンのXAMLコードです:私のスタイリッシュなボタン(xaml)をコードの後ろから呼び出す方法

 <Button x:Name="btnAddNewItem" 
      Grid.Column="0" Grid.Row="0" 
      FontSize="15" 
      BorderThickness="1.5" 
      HorizontalContentAlignment="Center" 
      VerticalContentAlignment="Center" 
      Foreground="White" 
      Background="White" 
      BorderBrush="#0091EA" Margin="5,0,0,0" Height="90" Width="90"> 
      <Button.Template> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border BorderThickness="{TemplateBinding BorderThickness}" 
      BorderBrush="{TemplateBinding BorderBrush}" 
      Background="{TemplateBinding Background}"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
        </Border> 
       </ControlTemplate> 
      </Button.Template> 

そして、私はこのような何かを達成したいと思いますので、私は同じスタイルで、XAMLのボタンを削除し、その必要なときにプログラム的に同じボタンを作成したいですそして、すべて私はそれを作成したい方法の例、XAMLで行ったように:

private void AddSubmenuButton(string name) 
{ 
      MyButton button = new MyButton(); 
      button.ButtonText = name; 
} 

が、このことは可能ですし、もしそうなら:どのように?

PS私はこのように、古典的な方法を試してみました:

Button a = new Button(); 
      a.BorderThickness = new Thickness(1); 
      a.Foreground = new SolidColorBrush(Colors.Black); 
      a.BorderBrush = mySolidColorBrush; 
      a.Content = "Button1"; 
      a.Width = 90; 
      a.Height = 90; 

しかし、私はこれを取得:

enter image description here

それはそれがすべてではない、それはいくつかの奇妙なを持っている1その厚さに気づくことが可能です値 と私はそれを変更する方法がわかりません..だから、私ははるかに良いと私はそれを呼び出す/コードの背後に作成したいxamlでボタンを作成した理由です。

EDIT:

MM8 @本当にありがとうございます! それは!

これは素晴らしい仲間が、私は私のボタンのアイコン+テキストを配置したい場合、私は をussualyということについての私のためにこのような何か、その罰金を追加するもの:あなたと

<Grid> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="80*"> 
    <RowDefinition Height="20*"/> 
</Grid.RowDefinitions> 
    <Image Source="/MyProject.Wpf;component/Icons/customer-icon.png" Margin="10" Grid.Row="0" Width="Auto"/> 
    <TextBlock Foreground="Black" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" >Izlaz</TextBlock> 
</Grid> 

私は私のボタンにグリッドを追加します見ることができ、それは次のようになります。

<Button x:Name="btnAddNewItem" 
        Grid.Column="0" Grid.Row="0" 
        FontSize="15" 
        ToolTip="Podaci" 
        BorderThickness="1.5" 
        HorizontalContentAlignment="Center" 
        VerticalContentAlignment="Center" 
        Foreground="White" 
        Background="White" 
        BorderBrush="#0091EA" Margin="5,0,0,0" Height="90" Width="90"> 
        <Button.Template> 
         <ControlTemplate TargetType="{x:Type Button}"> 
          <Border BorderThickness="{TemplateBinding BorderThickness}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        Background="{TemplateBinding Background}"> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
          </Border> 
         </ControlTemplate> 
        </Button.Template> 
     <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="80*"> 
      <RowDefinition Height="20*"/> 
     </Grid.RowDefinitions> 
      <Image Source="/MyProject.Wpf;component/Icons/customer-icon.png" Margin="10" Grid.Row="0" Width="Auto"/> 
      <TextBlock Foreground="Black" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" >Izlaz</TextBlock> 
     </Grid> 
        </Button> 

だから私は何とかもプログラム的にそれを作成するには、この(MYボタンの画像+テキスト)を達成できます。

+0

私はXAMLやものは分かりませんが、あなたはsthを実行したいと思っています。 'this.btnAddNewItem.Content = name'のように。 – nozzleman

+0

しかし、達成したいことを説明するときは、より具体的にする必要があります。 – nozzleman

+0

@nozzleman xamlボタンを使用して削除/削除をしたいのですが、同じボタンを作成することをインストゥルメントしましたが、この時間はプログラマチックに、必要なときだけ同じスタイルで、xamlで行ったように。 –

答えて

2

はあなたのApp.xamlでスタイルを定義します。

<Application x:Class="WpfApplication1.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApplication1" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <Style x:Key="theStyle" TargetType="Button"> 
      <Setter Property="FontSize" Value="15" /> 
      <Setter Property="BorderThickness" Value="1.5" /> 
      <Setter Property="HorizontalContentAlignment" Value="Center" /> 
      <!-- and so on for each property...--> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Border BorderThickness="{TemplateBinding BorderThickness}" 
      BorderBrush="{TemplateBinding BorderBrush}" 
      Background="{TemplateBinding Background}"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Application.Resources> 
</Application> 

あなたは、どちらのXAMLで、アプリケーション内の任意のボタンにこのスタイルを適用することができます。

<Button x:Name="btnAddNewItem" Style="{StaticResource myStyle}"/> 

...またはプログラムを:

Button button = new Button(); 
button.Style = Application.Current.Resources["myStyle"] as Style; 
+0

私の編集を確認してください! :) 再度、感謝します! –

+0

GridとImageおよびTextBlockをプログラムで作成し、ButtonのContentプロパティを設定することもできます。しかし、新しい問題がある場合は、新しい質問をしてください。問題の詳細を必ず記述してください。 – mm8

+0

私はあなたが仲間と言ったように、ここで私が試したものと実績を達成したものの例ですが、まだ私はここにこっそりしています.. http://stackoverflow.com/questions/41592928/how-to-add-grid- 2行と1つのコンテンツをボタンでプログラム可能にする –

関連する問題