6

当您在 wpf 中有一个用户控件时,它可以到达其父元素之外吗?例如,我的用户控件只是列出了一些通用的东西,这些东西保存在控件中,这些控件封装在主窗口的停靠面板中,但我在主窗口中有一个文本框和按钮,我想从控件中访问它们......这可能吗?

这将节省我很多时间,而不是更改整个窗口的内容并在每个用户控件中显示相同的文本框/按钮。如果有人有这方面的例子,将不胜感激。

4

3 回答 3

3

更好的设计模式是让用户控件在需要更改某些内容时(通过事件)通知主窗口,并在需要某些信息时询问窗口(通过方法)。例如,您将在用户控件可以调用的窗口上有一个 GetText() 方法,在窗口将订阅的用户控件上有一个 ChangeText 事件。

这个想法是始终控制窗口。使用这种心态,将来开发应用程序会更容易。

于 2012-04-17T23:59:14.450 回答
3

是的,这是可能的,这里是一些我用来从具有 DP 的 UserControls 组成演示文稿的代码。

我一点也不喜欢它,但它确实有效。我也认为这是一个很棒的话题,也许一些代码将有助于获得更好的答案!

干杯,
贝瑞

用户控件 XAML

<Button x:Name="btnAddNewItem" Style="{StaticResource blueButtonStyle}" >
    <StackPanel Orientation="Horizontal">
        <Image Source="{resx:Resx ResxName=Core.Presentation.Resources.MasterDetail, Key=bullet_add}" Stretch="Uniform" />
        <Label x:Name="tbItemName" Margin="5" Foreground="White" Padding="10, 0">_Add New [item]</Label>
    </StackPanel>
</Button>

用户控制代码背后

public partial class AddNewItemButton : UserControl
{
    ...

    #region Item Name

    public static readonly DependencyProperty ItemNameProperty = DependencyProperty.Register(
        "ItemName", typeof(string), typeof(AddNewItemButton),
        new FrameworkPropertyMetadata(OnItemNameChanged));

    public string ItemName
    {
        get { return (string)GetValue(ItemNameProperty); }
        set { SetValue(ItemNameProperty, value); }
    }

    public string ButtonText { get { return (string) tbItemName.Content; } }

    private static void OnItemNameChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        // When the item name changes, set the text of the item name
        var control = (AddNewItemButton)obj;

        control.tbItemName.Content = string.Format(GlobalCommandStrings.Subject_Add, control.ItemName.Capitalize());
        control.ToolTip = string.Format(GlobalCommandStrings.Subject_Add_ToolTip, control.ItemName);
    }

    #endregion

    #region Command

    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
        "Command", typeof(ICommand), typeof(AddNewItemButton),
        new FrameworkPropertyMetadata(OnCommandChanged));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    private static void OnCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        // When the item name changes, set the text of the item name
        var control = (AddNewItemButton)obj;
        control.btnAddNewItem.Command = control.Command;
    }

    #endregion

}

在此处输入图像描述

另一个显示合成的用户控件

<UserControl ...
         xmlns:uc="clr-namespace:Smack.Core.Presentation.Wpf.Controls.UserControls" 
         >

    <DockPanel LastChildFill="True">
        ...
        <uc:AddNewItemButton x:Name="_addNewItemButton" Margin="0,0,10 0" DockPanel.Dock="Right"  />
        ...
    </DockPanel>
</UserControl>

在此处输入图像描述

于 2012-04-18T00:46:43.113 回答
2

要回答您的问题:是的,您可以通过 RelativeSource 绑定或通过后端代码中的 Parent 成员访问父控件。但更好的答案类似于@KendallFrey 的答案。采用Light MVVM 之类的框架并使用其信使类或使用 Kendall 描述的事件。

于 2012-04-18T00:44:12.537 回答