VisualState Setter 与 CustomControl 不工作
VisualState Setter with CustomControl not working
我有一个包含按钮和图像控件的 UserControl 以及一个 属性,如下所示:
public sealed partial class ImageButton : UserControl
{
public ImageSource Source { get { return Image.Source; } set { Image.Source = value; } }
}
在 XAML 中设置 ImageSource 可以像这样正常工作:
<views:ImageButton x:Name="MyButton" Source="../Assets/image.jpg"/>
但是当我尝试在 VisualStateManager 中设置它时,它破坏了完整的状态:
<Setter Target="MyButton.Source" Value="../Assets/image.jpg"/>
像往常一样 Windows 没有(有用的)错误消息,所以我不知道这里出了什么问题。有人可以帮忙吗?
好吧,我自己发现了:你需要将 Source 属性 注册为 Dependency属性
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;
}
您应该使用依赖性 属性。下面是一个用于详细说明的示例解决方案:
用户控制
<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>
后面的用户控制代码
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)));
}
Parent XAML
<StackPanel Margin="100,10,10,10">
<local:ImageButton Source="../Assets/SplashScreen.scale-200.png" ButtonContent="Test Button!"/>
</StackPanel>
我有一个包含按钮和图像控件的 UserControl 以及一个 属性,如下所示:
public sealed partial class ImageButton : UserControl
{
public ImageSource Source { get { return Image.Source; } set { Image.Source = value; } }
}
在 XAML 中设置 ImageSource 可以像这样正常工作:
<views:ImageButton x:Name="MyButton" Source="../Assets/image.jpg"/>
但是当我尝试在 VisualStateManager 中设置它时,它破坏了完整的状态:
<Setter Target="MyButton.Source" Value="../Assets/image.jpg"/>
像往常一样 Windows 没有(有用的)错误消息,所以我不知道这里出了什么问题。有人可以帮忙吗?
好吧,我自己发现了:你需要将 Source 属性 注册为 Dependency属性
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;
}
您应该使用依赖性 属性。下面是一个用于详细说明的示例解决方案:
用户控制
<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>
后面的用户控制代码
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)));
}
Parent XAML
<StackPanel Margin="100,10,10,10">
<local:ImageButton Source="../Assets/SplashScreen.scale-200.png" ButtonContent="Test Button!"/>
</StackPanel>