【问题标题】:Bind control property from the inner control从内部控件绑定控件属性
【发布时间】:2015-06-09 11:06:00
【问题描述】:

我有一个包含另一个控件的控件。 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 绑定到MainControl 上的MyButton

【问题讨论】:

    标签: c# wpf xaml templates winrt-xaml


    【解决方案1】:

    你需要直接绑定InnerControl的“ButtonVisibility”属性,像这样:

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

    【讨论】:

    • 詹姆斯,你能帮我解决另一个问题吗?如果 MyInner 为 null,如何将 Button Visibility 设置为自动折叠?我尝试设置 TargetNullValue=Collapsed,但它在 WinRT 中不起作用
    • @PavelGriza 而不是 null,你可以让它有一个默认值 (new PropertyMetadata(, OnMyInnerPropertyChanged ...
    【解决方案2】:

    就像@james 提到的那样,您必须使用 RelativeSource 来绑定到 TemplatedParent 或通过指定如下类型来绑定到可视树中的任何类型:

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

    【讨论】:

    • 请注意 winrt-xaml 中不支持 x:Type。 OP 没有具体说明他正在写什么平台,但我认为这会很有用。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 2013-02-18
      • 2012-07-18
      • 1970-01-01
      相关资源
      最近更新 更多