请问大神一条关于wpf的有关问题
请教大神一条关于wpf的问题
<Application.Resources>
<Style x:Key="btnstyle2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="50" Height="60">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Name="image"/>
<Label Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
我写了这样的一段代码,就是想用ContentControl来改变Button的外观,我定义好几个Button,都是采用这种样式(上面是图片,下面是文字),但是我每个Button的图片和文字都不相同,然后我在xaml里定义Button的时候就遇到问题了,怎么为我定义的这种样式的按钮添加图片和文字?求大神指教
------解决思路----------------------
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string Src
{
get
{
return "ic_launcher-web.png";
}
set { }
}
<Window Name="www" x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="btnstyle2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="50" Height="60">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="{Binding Src}"></Image>
<TextBlock Text="{Binding T}" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="ssssss" DataContext="{Binding ElementName=www}" Name="sssssss" Style="{StaticResource btnstyle2}"></Button>
</Grid>
</Window>
------解决思路----------------------
<Window Name="www" x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="btnstyle2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="50" Height="60">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="{Binding Src}"></Image>
<TextBlock Text="{TemplateBinding Content}" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="ssssss" DataContext="{Binding ElementName=www}" Name="sssssss" Style="{StaticResource btnstyle2}"></Button>
</Grid>
</Window>
<Application.Resources>
<Style x:Key="btnstyle2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="50" Height="60">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Name="image"/>
<Label Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
我写了这样的一段代码,就是想用ContentControl来改变Button的外观,我定义好几个Button,都是采用这种样式(上面是图片,下面是文字),但是我每个Button的图片和文字都不相同,然后我在xaml里定义Button的时候就遇到问题了,怎么为我定义的这种样式的按钮添加图片和文字?求大神指教
------解决思路----------------------
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string Src
{
get
{
return "ic_launcher-web.png";
}
set { }
}
<Window Name="www" x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="btnstyle2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="50" Height="60">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="{Binding Src}"></Image>
<TextBlock Text="{Binding T}" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="ssssss" DataContext="{Binding ElementName=www}" Name="sssssss" Style="{StaticResource btnstyle2}"></Button>
</Grid>
</Window>
------解决思路----------------------
<Window Name="www" x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="btnstyle2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="50" Height="60">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="{Binding Src}"></Image>
<TextBlock Text="{TemplateBinding Content}" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="ssssss" DataContext="{Binding ElementName=www}" Name="sssssss" Style="{StaticResource btnstyle2}"></Button>
</Grid>
</Window>