2014-07-02 43 views
0

這是我的用戶文件:設置文本屬性按鈕不起作用

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

然而文本和圖像源不在按鈕上!他們沒有出現。我究竟做錯了什麼?

+0

改變名稱= 「btnBack1」 爲x:名稱= 「btnBack1」 這也許不是這樣,但它是更安全 – Teaman

+1

並添加名稱= 「ImagedButton」 你的用戶控件 – Teaman

+1

*改變名稱= 「btnBack1」到x:Name =「btnBack1」它可能不是這種情況,但它更安全* ...是什麼讓你認爲這樣做*更安全*? – Sheridan

回答

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#代碼

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

0

你已經命名了你的UserControlImageControl,但是你已經嘗試使用名稱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> 

這還假定你已經正確申報正確的類型,命名爲您UserControlImagesSourceText兩個DependencyProperty S和已經爲它們設置適當的值。