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;
しかし、私はこれを取得:
それはそれがすべてではない、それはいくつかの奇妙なを持っている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ボタンの画像+テキスト)を達成できます。
私はXAMLやものは分かりませんが、あなたはsthを実行したいと思っています。 'this.btnAddNewItem.Content = name'のように。 – nozzleman
しかし、達成したいことを説明するときは、より具体的にする必要があります。 – nozzleman
@nozzleman xamlボタンを使用して削除/削除をしたいのですが、同じボタンを作成することをインストゥルメントしましたが、この時間はプログラマチックに、必要なときだけ同じスタイルで、xamlで行ったように。 –