FlipView Datatemplate 无法绑定到自定义 UserControl
FlipView Datatemplate fails to bind on custom UserControl
TL;DR
将 MahApps 的 FlipView
与自定义 DataTemplate
结合使用,在使用自定义 UserControl 时绑定无法更新。
问题
尝试使用 MahApps 的 FlipView 为每个视图托管自定义 UserControl。出于测试目的,我的 UserControl 看起来像这样(在 MetroDemo sample project 上测试)
XAML
<UserControl x:Class="MetroDemo.ExampleViews.CustomImageControl"
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:local="clr-namespace:MetroDemo.ExampleViews"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="ControlContainer">
<Image Source="{Binding SourceImage}" Stretch="Fill"/>
</Grid>
</UserControl>
代码隐藏
namespace MetroDemo.ExampleViews
{
/// <summary>
/// Interaction logic for CustomImageControl.xaml
/// </summary>
public partial class CustomImageControl : UserControl
{
public ImageSource SourceImage
{
get => (ImageSource)this.GetValue(SourceImageProperty);
set
{
this.SetValue(SourceImageProperty, value);
}
}
public static readonly DependencyProperty SourceImageProperty = DependencyProperty.Register(
nameof(SourceImage),
typeof(ImageSource),
typeof(CustomImageControl),
new PropertyMetadata(OnSourceImageChanged));
private static void OnSourceImageChanged(DependencyObject data, DependencyPropertyChangedEventArgs args)
{
var control = (CustomImageControl)data;
if (control is null || !(args.NewValue is ImageSource image))
return;
control.SourceImage = image;
}
public CustomImageControl()
{
InitializeComponent();
this.ControlContainer.DataContext = this;
}
}
}
然后,我在 FlipView 使用的 DataTemplate
中使用这个自定义用户控件,将 this bit here 替换为:
<DataTemplate x:Key="ImageDataTemplate" x:Shared="False">
<local:CustomImageControl SourceImage="{Binding Mode=OneWay, FallbackValue={x:Static DependencyProperty.UnsetValue}, Converter={mah:NullToUnsetValueConverter}}" />
</DataTemplate>
当翻转到下一个视图时,它会进行动画处理,但由于某种原因,它会保留相同的第一个绑定图像。
似乎绑定在某处失败了,但我看不到哪里。非常感谢任何帮助!
如果我注释掉以下内容,它会按预期工作:
private static void OnSourceImageChanged(DependencyObject data, DependencyPropertyChangedEventArgs args)
{
//var control = (CustomImageControl)data;
//if (control is null || !(args.NewValue is ImageSource image))
// return;
//control.SourceImage = image;
}
它基本上得到一个新值并将其设置为一个新的 ImageSource
,它将调用 OnSourceImageChanged
。在此空白中,它设置了一个新的 ImageSource
,它将再次调用此 OnSourceImageChanged
。
TL;DR
将 MahApps 的 FlipView
与自定义 DataTemplate
结合使用,在使用自定义 UserControl 时绑定无法更新。
问题
尝试使用 MahApps 的 FlipView 为每个视图托管自定义 UserControl。出于测试目的,我的 UserControl 看起来像这样(在 MetroDemo sample project 上测试)
XAML
<UserControl x:Class="MetroDemo.ExampleViews.CustomImageControl"
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:local="clr-namespace:MetroDemo.ExampleViews"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="ControlContainer">
<Image Source="{Binding SourceImage}" Stretch="Fill"/>
</Grid>
</UserControl>
代码隐藏
namespace MetroDemo.ExampleViews
{
/// <summary>
/// Interaction logic for CustomImageControl.xaml
/// </summary>
public partial class CustomImageControl : UserControl
{
public ImageSource SourceImage
{
get => (ImageSource)this.GetValue(SourceImageProperty);
set
{
this.SetValue(SourceImageProperty, value);
}
}
public static readonly DependencyProperty SourceImageProperty = DependencyProperty.Register(
nameof(SourceImage),
typeof(ImageSource),
typeof(CustomImageControl),
new PropertyMetadata(OnSourceImageChanged));
private static void OnSourceImageChanged(DependencyObject data, DependencyPropertyChangedEventArgs args)
{
var control = (CustomImageControl)data;
if (control is null || !(args.NewValue is ImageSource image))
return;
control.SourceImage = image;
}
public CustomImageControl()
{
InitializeComponent();
this.ControlContainer.DataContext = this;
}
}
}
然后,我在 FlipView 使用的 DataTemplate
中使用这个自定义用户控件,将 this bit here 替换为:
<DataTemplate x:Key="ImageDataTemplate" x:Shared="False">
<local:CustomImageControl SourceImage="{Binding Mode=OneWay, FallbackValue={x:Static DependencyProperty.UnsetValue}, Converter={mah:NullToUnsetValueConverter}}" />
</DataTemplate>
当翻转到下一个视图时,它会进行动画处理,但由于某种原因,它会保留相同的第一个绑定图像。 似乎绑定在某处失败了,但我看不到哪里。非常感谢任何帮助!
如果我注释掉以下内容,它会按预期工作:
private static void OnSourceImageChanged(DependencyObject data, DependencyPropertyChangedEventArgs args)
{
//var control = (CustomImageControl)data;
//if (control is null || !(args.NewValue is ImageSource image))
// return;
//control.SourceImage = image;
}
它基本上得到一个新值并将其设置为一个新的 ImageSource
,它将调用 OnSourceImageChanged
。在此空白中,它设置了一个新的 ImageSource
,它将再次调用此 OnSourceImageChanged
。