将文本属性设置为按钮不起作用

时间:2014-07-02 14:47:05

标签: wpf button user-controls

这是我的UserControl文件:

<UserControl x:Class="myProject.ButtonWithImage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:myProject="clr-namespace:myProject;assembly=myProject.BusinessLogic"
         mc:Ignorable="d" 
         Name="ImagedControl"
         d:DesignHeight="300" d:DesignWidth="300">
<Button Height="35" Width="90" FocusVisualStyle="{x:Null}" Foreground="White"
        Click="OnClick" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0 0 1 1">
    <StackPanel Orientation="Horizontal" Margin="3">
        <Image Source="{Binding ElementName=ImagedButton, Path=ImageSource}" Stretch="None" Margin="0 0 5 0" />
        <TextBlock Text="{Binding ElementName=ImagedButton, Path=Text}" FontSize="12" VerticalAlignment="Center" />
    </StackPanel>
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="border" CornerRadius="8">
                <Border.Background>
                    SlateBlue
                </Border.Background>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

在页面中我设置了一个这样的按钮:

<myProject:ButtonWithImage ImageSource="/Resources/test.png" Text="Back" Name="btnBack1" Command="NavigationCommands.GoToPage" CommandParameter="ViewModel/Categories.xaml" />

然而,文本和imagesource不在按钮上!它们没有出现。我做错了什么?

2 个答案:

答案 0 :(得分:1)

创建按钮用户控件

<Button x:Class="WpfApplication5.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
    FocusVisualStyle="{x:Null}" Content="ok" Foreground="White" Height="30" Width="90" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0 0 1 1">   
<Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
        <Border x:Name="border" CornerRadius="8">
            <Border.Background>
                SlateBlue
            </Border.Background>
            <StackPanel Orientation="Horizontal" Margin="3">
                <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Height="30" Width="30" Stretch="Fill" Margin="0 0 0 0" />
                <TextBlock Text="{Binding Path=Content,RelativeSource={RelativeSource TemplatedParent}}" FontSize="12" VerticalAlignment="Center" />
            </StackPanel>
        </Border>
    </ControlTemplate>
</Button.Template>


c#c​​ode

 public partial class UserControl1 : Button
{
    public UserControl1()
    {
        InitializeComponent();
    }
}

xaml窗口

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:myProject="clr-namespace:WpfApplication5"
    Title="MainWindow" Height="350" Width="525">

<myProject:UserControl1  Content="Button Text" Tag="btn2.png" />

答案 1 :(得分:0)

您已将UserControl ImageControl命名为,但之后您尝试使用名称ImageButton在XAML中访问它。如果坚持使用这个名字,你会有更多的运气。试试这个:

<UserControl x:Class="myProject.ButtonWithImage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:myProject="clr-namespace:myProject;assembly=myProject.BusinessLogic"
         mc:Ignorable="d" 
         Name="ImagedControl"
         d:DesignHeight="300" d:DesignWidth="300">
<Button Height="35" Width="90" FocusVisualStyle="{x:Null}" Foreground="White"
        Click="OnClick" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0 0 1 1">
    <StackPanel Orientation="Horizontal" Margin="3">
        <Image Source="{Binding ElementName=ImagedControl, Path=ImageSource}" Stretch="None" Margin="0 0 5 0" />
        <TextBlock Text="{Binding ElementName=ImagedControl, Path=Text}" FontSize="12" VerticalAlignment="Center" />
    </StackPanel>
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="border" CornerRadius="8">
                <Border.Background>
                    SlateBlue
                </Border.Background>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

这也假定您已在DependencyProperty中正确声明了两个ImagesSource正确类型TextUserControl,并为它们设置了适当的值。