0

ich ein Usercontrol hava, die einen Knopf und ein Image Control und eine Eigenschaft wie folgt enthält:Visual Setter mit Custom nicht funktioniert

public sealed partial class ImageButton : UserControl 
     { 
      public ImageSource Source { get { return Image.Source; } set { Image.Source = value; } } 
     } 

die Image in XAML Einstellung funktioniert wie folgt fein:

<views:ImageButton x:Name="MyButton" Source="../Assets/image.jpg"/> 

Aber wenn ich es in Visual zu setzen versuche, bricht es den kompletten Zustand:

<Setter Target="MyButton.Source" Value="../Assets/image.jpg"/> 

Wie üblich Fenster s spuckt keine (hilfreiche) Fehlermeldung aus, daher habe ich keine Ahnung was hier falsch ist. Kann jemand helfen?

Antwort

0

Okay, fand es heraus selbst: Sie müssen die Source-Eigenschaft als DependencyProperty

public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", 
      typeof (ImageSource), typeof (ImageButton), new PropertyMetadata(string.Empty, OnPropertyChanged)); 

private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
      { 
       ((ImageButton) d).Source = (ImageSource) e.NewValue; 
      } 
0

registrieren Sie sollten Abhängigkeitseigenschaft verwenden. Im Folgenden finden Sie eine Probenlösung für die Ausarbeitung:

Benutzerverwaltung

<UserControl 
x:Class="TestApps.ImageButton" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:TestApps" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" 
d:DesignWidth="400" x:Name="MyControl"> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Image Source="{Binding ElementName=MyControl, Path=Source, Mode=OneWay}"/> 
    <Button Content="{Binding ElementName=MyControl, Path=ButtonContent, Mode=OneWay}" 
      Grid.Row="1" Height="50" HorizontalAlignment="Center"/> 
</Grid> 
</UserControl> 

Benutzersteuercode hinter

public sealed partial class ImageButton : UserControl 
{ 
    public ImageButton() 
    { 
     this.InitializeComponent(); 
    } 

    public string Source 
    { 
     get { return (string)GetValue(SourceProperty); } 
     set { SetValue(SourceProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty SourceProperty = 
     DependencyProperty.Register("Source", typeof(string), typeof(ImageButton), new PropertyMetadata(default(string))); 

    public string ButtonContent 
    { 
     get { return (string)GetValue(ButtonContentProperty); } 
     set { SetValue(ButtonContentProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ButtonContentProperty = 
     DependencyProperty.Register("ButtonContent", typeof(string), typeof(ImageButton), new PropertyMetadata(default(string))); 
} 

Eltern XAML

<StackPanel Margin="100,10,10,10"> 
     <local:ImageButton Source="../Assets/SplashScreen.scale-200.png" ButtonContent="Test Button!"/> 
    </StackPanel> 

Ausgabe enter image description here

Verwandte Themen