Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 将文本属性设置为按钮不会';行不通_Wpf_Button_User Controls - Fatal编程技术网

Wpf 将文本属性设置为按钮不会';行不通

Wpf 将文本属性设置为按钮不会';行不通,wpf,button,user-controls,Wpf,Button,User Controls,这是我的用户控制文件: <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.openxmlformat

这是我的用户控制文件:

<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" />


但是,文本和图像源不在按钮上!他们没有出现。我做错了什么?

您已经命名了
UserControl
ImageControl
,但是您尝试使用name
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
Text
,并为它们设置了适当的值。

已创建按钮用户控件

<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>
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" />


将Name=“btnBack1”更改为x:Name=“btnBack1”可能不是这样,但它是安全的,并将Name=“ImagedButton”添加到您的用户控件将Name=“btnBack1”更改为x:Name=“btnBack1”可能不是这样,但更安全。。。是什么让您认为这样做更安全?至少在VS2010、VS2012、VS2013中,在使用.Net 3.5时,您可能会遇到这样一种情况,即指定了Name属性的UserControl将导致VS在输出窗口中生成带有奇怪描述的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" />