简体   繁体   English

如何将 UserControl 中的嵌套控件完全暴露给外部?

[英]How do I fully expose a nested control in a UserControl to the outside?

Let's say I have a very simple UserControl :假设我有一个非常简单的UserControl

<UserControl x:Class="Test.UserControls.MyControl">
    <DockPanel>
        <Label Content="lorem" />
        <Button Name="ButtonFromUserControl" />
    </DockPanel>
</UserControl>

For example, I want to access all properties of the Button from the outside if I add the UserControl in another file.例如,如果我在另一个文件中添加UserControl ,我想从外部访问Button所有属性。 I'm imagining something like this:我在想象这样的事情:

 <StackPanel>
     <usercontrols:MyControl Button.Dock="{Binding ButtonDockPosition}"/>
 </StackPanel>

I know that I can create dependency properties for each property to make them accessible, however I don't know yet which may or may not be used later and don't want to add a DP for each of the nested controls' properties.我知道我可以为每个属性创建依赖属性以使其可访问,但是我还不知道以后可能会或可能不会使用哪些属性,并且不想为每个嵌套控件的属性添加 DP。

I'm not sure if this would be the correct way to do it, if there is another way or pattern I'd be interested as well.我不确定这是否是正确的方法,如果有另一种方式或模式我也会感兴趣。

Edit:编辑:

I also tried to make the Button itself a DP, but that doesnt't let me access its properties:我还尝试使Button本身成为 DP,但这不允许我访问其属性:

public static readonly DependencyProperty NestedButtonProperty =
    DependencyProperty.RegisterAttached("NestedButton", typeof (Button), typeof (MyControl),
        new FrameworkPropertyMetadata());
     
     
 public Button NestedButton => ButtonFromUserControl;

I have found a kind of workaround which at least doesn't require me to generate huge amounts of forwarding DPs.我找到了一种至少不需要我生成大量转发 DP 的解决方法。 Since most (or all?) of the controls' properties can be also set via a Style , I simply made the Style of each control a DP:由于大多数(或全部?)控件的属性也可以通过Style设置,我只是将每个控件的Style设置为 DP:

public static readonly DependencyProperty ButtonStyleProperty =
    DependencyProperty.RegisterAttached("ButtonStyle", typeof (Style), typeof (MyUserControl),
        new FrameworkPropertyMetadata());

 public Style ButtonStyle
 {
     get { return (Style) GetValue(ButtonStyleProperty); }
     set { SetValue(ButtonStyleProperty, value); }
 }

Bind it in the UserControl :UserControl绑定它:

 <Button Name="InnerButton" Style="{Binding ButtonStyle, ElementName=MyUserControl}" />

Now you can set the properties of the Button with a custom style:现在您可以使用自定义样式设置Button的属性:

 <userControls:MyUserControl>
    <userControls:MyUserControl.ButtonStyle>
        <Style TargetType="Button">
            <Style.Setters>
                <Setter Property="Content" Value="ButtonContent" />
                <Setter Property="BorderThickness" Value="5" />
                <Setter Property="BorderBrush" Value="Orange" />
            </Style.Setters>
        </Style>
    </userControls:MyUserControl.ButtonStyle>
  </userControls:MyUserControl>
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM