UserControl using parent elements in wpf?

When you have usercontrol in wpf, can it go beyond its parent elements? For example, my user control simply lists some common things that are stored inside the control that is encapsulated in the dock in the main window, but I have a text box and a button in the main window that I would like to access from the control ... is it possible?

This will save me time and not change the contents of the whole window and show the same text field / button in each usercontrol. If anyone has an example of this, that would be very appreciated.

+6
source share
3 answers

Yes, it is possible, and here is some code that I used to compose presentations from UserControls with DP.

I don't like it much, but it works. I also think this is a great topic, and maybe some code will help get the best answers!

Cheers
Berry

UserControl 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> 

UserControl Behind Code

 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 } 

enter image description here

Another UserControl element showing composition

 <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> 

enter image description here

+3
source

The best design pattern would be to force usercontrol to notify (via an event) the main window when something needs to be changed, and request a window (via a method) when it needs some information. For example, you have a GetText () method in a window that can trigger user control, and a ChangeText event in usercontrol that the window will subscribe to.

The idea is to constantly monitor the window. Using this mentality will make it easier for you to develop applications in the future.

+3
source

To answer your question: yes, you can either access the parent controls, either through the RelativeSource binding, or through the Parent member in the reverse code. But the best answer is similar to @KendallFrey's answer. Accept the framework as Light MVVM and use its messenger class or use events as Kendall described.

+2
source

Source: https://habr.com/ru/post/913472/


All Articles