从内部控件绑定控件属性

时间:2015-06-09 11:06:00

标签: c# wpf xaml templates winrt-xaml

我有一个包含另一个控件的控件。 InnerControl包含一些像ButtonVisibility这样的属性。简化的InnerControl代码示例位于

之下
public class InnerControl : ControlBase
{
    //...
    public Visibility ButtonVisibility
    {
        get { return (Visibility)GetValue(ButtonVisibility); }
        set { SetValue(ButtonVisibility, value); }
    }

    public static readonly DependencyProperty ButtonVisibility =
        DependencyProperty.Register(
            "ButtonVisibility",
            typeof (Visibility),
            typeof (InnerControl),
            new PropertyMetadata(Visibility.Collapsed));
}

这是MainControl的代码,其中包含InnerControl作为依赖属性

public class MainControl : ControlBase
{
    //...
    public InnerControl MyInner
    {
        get { return (InnerControl) GetValue(MyInnerProperty); }
        set { SetValue(MyInnerProperty, value); }
    }

    public static readonly DependencyProperty MyInnerProperty =
        DependencyProperty.Register(
            "MyInner",
            typeof (InnerControl),
            typeof (MainControl),
            new PropertyMetadata(null, OnMyInnerPropertyChanged));

    private static void OnMyInnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //...
    }
}

我在Generic.xaml

中有两个不同的模板用于此控件
<Style TargetType="local:InnerControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:InnerControl">
                <Grid x:Name="ContainerGrid">
                    <!-- some markup -->
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="local:MainControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MainControl">
                <Grid x:Name="RootGrid">

                    <Button x:Name="MyButton" Visibility="{TemplateBinding ???}"/>

                    <ContentControl x:Name="MyInnerControl" Content="{TemplateBinding MyInner}" />

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我如何使用此控件的方式

<controls:MainControl>
    <controls:MainControl.MyInner>
        <controls:InnerControl ButtonVisibility="Visible">
            <!-- some markup -->                
        </controls:InnerControl>
    </controls:MainControl.MyInner>

    <!-- some markup -->
</controls:MainControl>

我需要在按钮的点击后更改MainControl的状态。这就是它位于MainControl模板中的原因。

那么,任何机构都可以告诉我如何将ButtonVisibilityMyInner绑定到MyButton上的MainControl吗?

3 个答案:

答案 0 :(得分:2)

您需要直接绑定到InnerControl的“ButtonVisibility”属性,如下所示:

<Button x:Name="MyButton" 
        Visibility="{Binding MyInner.ButtonVisibility, 
                             RelativeSource={RelativeSource TemplatedParent}}"/>

答案 1 :(得分:1)

与@james一样,您必须通过指定类型来使用RelativeSource绑定到TemplatedParent或可视树中的任何Type:

<Button x:Name="MyButton" Content="MyButton" 
       Visibility="{Binding Path=MyInner.ButtonVisibility, 
                            RelativeSource={RelativeSource FindAncestor,
                                           AncestorType={x:Type controls:MainControl}}}"/>

答案 2 :(得分:-1)

WPF AttachedProperty有助于从内部控件更改主控件的状态。