如何使XAML设计器(VS 2010)将UserControl属性绑定到内部XAML元素?

时间:2021-07-30 14:10:24

I created a UserControl with orientation property. All the orientation work should be done by inner stack panel, so I bound it to the property.

我创建了一个带有orientation属性的UserControl。所有的定位工作都应该由内部堆栈面板完成,所以我将它绑定到属性。

This works fine at run time, but the WPF designer shows default value of the stack panel ( vertical ) instead of the default value of the orientation property ( horizontal ).

这在运行时工作正常,但WPF设计器显示堆栈面板的默认值(垂直)而不是orientation属性的默认值(水平)。

Here is simplified example. XAML:

这是简化的例子。 XAML:

<UserControl x:Class="MyWpf.MyUserControl"
             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:MyWpf="clr-namespace:MyWpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="{Binding Path=Orientation, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyWpf:MyUserControl}}}">
        <TextBlock>A</TextBlock>
        <TextBlock>B</TextBlock>
        <TextBlock>C</TextBlock>
    </StackPanel>
</UserControl>

Code behind:

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty OrientationProperty;

    static MyUserControl()
    {
        OrientationProperty = DependencyProperty.Register( "Orientation", typeof( Orientation ), typeof( MyUserControl ), new FrameworkPropertyMetadata( Orientation.Horizontal ) );
    }

    public Orientation Orientation
    {
        get
        {
            return (Orientation)GetValue( OrientationProperty );
        }
        set
        {
            SetValue( OrientationProperty, value );
        }
    }

    public MyUserControl()
    {
        InitializeComponent();
    }
}

The only solution I see is to include the Orientation into ViewModel (and use DesignData), but I want it to be clean. I hope there is something like DesignData, but for the control itself.

我看到的唯一解决方案是将Orientation包含到ViewModel中(并使用DesignData),但我希望它是干净的。我希望有类似DesignData的东西,但对于控件本身。

1 个解决方案

#1


0  

Maybe this could help

也许这可能有所帮助

What approaches are available to dummy design-time data in WPF?

WPF中虚拟设计时数据有哪些方法可用?

I'm guessing you could try setting d:Orientation on your StackPanel.

我猜你可以尝试在你的StackPanel上设置d:Orientation。

Most Binding extensions work their magic at run-time (sometimes you need that, some times at compile time). I'm not 100% but you could try with {Binding ElementName=_userControl, Path=Orientation} - but even if it 'works' I don't think that'd work for you since that's your custom property.

大多数Binding扩展在运行时都会发挥作用(有时你需要它,有些时候在编译时)。我不是100%,但你可以尝试使用{Binding ElementName = _userControl,Path = Orientation} - 但即使它“有效”,我也不认为这对你有用,因为那是你的自定义属性。

Here is a link about something similar which explains a bit (but doesn't help your case).
WPF TemplateBinding vs RelativeSource TemplatedParent

这是一个类似的东西的链接,解释了一点(但没有帮助您的情况)。 WPF TemplateBinding与RelativeSource TemplatedParent

#1


0  

Maybe this could help

也许这可能有所帮助

What approaches are available to dummy design-time data in WPF?

WPF中虚拟设计时数据有哪些方法可用?

I'm guessing you could try setting d:Orientation on your StackPanel.

我猜你可以尝试在你的StackPanel上设置d:Orientation。

Most Binding extensions work their magic at run-time (sometimes you need that, some times at compile time). I'm not 100% but you could try with {Binding ElementName=_userControl, Path=Orientation} - but even if it 'works' I don't think that'd work for you since that's your custom property.

大多数Binding扩展在运行时都会发挥作用(有时你需要它,有些时候在编译时)。我不是100%,但你可以尝试使用{Binding ElementName = _userControl,Path = Orientation} - 但即使它“有效”,我也不认为这对你有用,因为那是你的自定义属性。

Here is a link about something similar which explains a bit (but doesn't help your case).
WPF TemplateBinding vs RelativeSource TemplatedParent

这是一个类似的东西的链接,解释了一点(但没有帮助您的情况)。 WPF TemplateBinding与RelativeSource TemplatedParent