WPF 嵌套用户控件依赖绑定属性

问题描述 投票:0回答:1

我有一个使用 Prism 和 ViewModels 的 WPF 应用程序,我在使用具有自己的 CodeBehind 和 DependencyBinding 属性的嵌套 UserControl 时遇到问题。

使用此代码,PanZoomControl 中的绑定不起作用,图像未设置,并且在调试中

get
set
未被调用。

ImagePreviewView.xaml

<UserControl x:Class="Modules.ImagePreviewView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:prism="http://prismlibrary.com/"
         xmlns:controls="clr-namespace:UI.Infrastructure.Controls;assembly=UI.Infrastructure"
         prism:ViewModelLocator.AutoWireViewModel="True">
<x:Code>
    <![CDATA[ public ImagePreviewView() { InitializeComponent(); } ]]>
</x:Code>

<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <controls:PanZoomControl Grid.RowSpan="3"
                             Grid.ColumnSpan="3" 
                             ImageSrc="{Binding ImagePreviewVM.ImagePreview}">
    </controls:PanZoomControl>

</Grid>

PanZoomControl.xaml

<UserControl x:Class="UI.Infrastructure.Controls.PanZoomControl"
             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="450" d:DesignWidth="800">
    <Grid>
        <Image Source="{Binding ImageSrc}" />
    </Grid>
</UserControl>

PanZoomControl.xaml.cs

public partial class PanZoomControl : UserControl
{
    #region Dependency Properties
    public static readonly DependencyProperty ImageSourceProperty =
       DependencyProperty.Register("ImageSrc", typeof(object), typeof(PanZoomControl));

    public object ImageSrc
    {
        get { return GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }
    
    #endregion

    public PanZoomControl()
    {
        InitializeComponent();
    }
}
c# wpf user-controls prism
1个回答
0
投票

ImageSrc
不是DataContext的属性,它是控件本身的属性,所以更改绑定源:

<UserControl x:Class="UI.Infrastructure.Controls.PanZoomControl"
             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" 
             Name="uc"
             mc:Ignorable="d"  
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Image Source="{Binding ImageSrc, ElementName=uc}" />
    </Grid>
</UserControl>

另外,既然DP是用名字

ImageSrc
注册的,那么它应该被命名为
ImageSrcProperty

© www.soinside.com 2019 - 2024. All rights reserved.