Wpf User Control Parent

Binding wpf user control to parent property

The DataContext for the DataTrigger is set to the window, that's why it looks at the window for "State". You just need to tell the binding that State is on the user control. Try this:

<DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="True">

Here is a complete example:

MainWindow.xaml

<Window x:Class="WpfApplication89.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication89"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:UserControl1 State="{Binding Path=DualState}" />
<CheckBox Content="DualState" IsChecked="{Binding DualState}" />
</StackPanel>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace WpfApplication89
{
public partial class MainWindow : Window
{
public static readonly DependencyProperty DualStateProperty = DependencyProperty.Register("DualState", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));

public bool DualState
{
get { return (bool)GetValue(DualStateProperty); }
set { SetValue(DualStateProperty, value); }
}

public MainWindow()
{
InitializeComponent();
}
}
}

UserControl1.xaml

<UserControl x:Class="WpfApplication89.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication89"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="User Control 1">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Beige" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="true">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>

UserControl1.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication89
{
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(bool), typeof(UserControl1), new PropertyMetadata(false));

public bool State
{
get { return (bool)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}

public UserControl1()
{
InitializeComponent();
}
}
}

MainWindow.xaml.cs (INotifyPropertyChanged version)

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApplication89
{
public partial class MainWindow : Window, INotifyPropertyChanged
{

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName]string name = null)
{
if (Equals(field, value))
{
return false;
}
field = value;
this.OnPropertyChanged(name);
return true;
}
protected void OnPropertyChanged([CallerMemberName]string name = null)
{
var handler = this.PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion

#region Property bool DualState
private bool _DualState;
public bool DualState { get { return _DualState; } set { SetProperty(ref _DualState, value); } }
#endregion

public MainWindow()
{
InitializeComponent();
}
}
}

How to Access Parent Window's Control from user control window

I found the answer finally

Window parentWindow = Window.GetWindow(this);

object obj= parentWindow.FindName("Popup1");
System.Windows.Controls.Primitives.Popup pop = (System.Windows.Controls.Primitives.Popup)obj;

pop.IsOpen = false;

How to close parent windows using WPF User Control

Inside the custom control that you've created. You can access the parent window from within the button event click.

Either by using the visual tree:

var myWindow = (Window)VisualParent.GetSelfAndAncestors().FirstOrDefault(a => a is Window);
myWindow.Close();

or by simply:

var myWindow = Window.GetWindow(this);
myWindow.Close();

Of course, the other option would be to create a custom event that says "MyButtonClicked" and then have the window that hosts the UserControl to listen to this event and when the Event is fired, you close the current window.

Cheers!

WPF UserControl Triggers from parent window

...but is it possible to do this when the EditButton is within the parent window?

No, it's not because then the Button and TextBox belong to different naming scopes.

It is my understanding that the data context for the usercontrol will be inherited from the parent window, so is it possible to do it as simple as a trigger bound to the button or will I have to use a viewmodel/button commands?

You should bind the IsChecked property of the ToogleButton to a source property of a view model, and then bind the trigger to same source property:

<DataTrigger Binding="{Binding IsChecked}" Value="True">
...

Make sure that the view model implements the INotifyPropertyChanged interface correctly: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

C# / WPF - Getting object inside a child user control from parent window

It definitely looks like the design should be improved, but just to get this working you can do the following:

    private void Submit_Btn_Click(object sender, RoutedEventArgs e)
{
var controlAsUserControl1 = MainContentControl.Content as UserControl1;
if (controlAsUserControl1 != null)
{
Debug.WriteLine(controlAsUserControl1.TxtBox1.Text);
}
}

How to pass a string from user control to its parent Window in WPF?

You could easily achieve what you need by using a property in the UC and subscribe to the propertychanged event of this property in the MainWindow.

In UserControl

public class UserControl : BindableBase
{

private string textboxText;
public string TextBoxText
{
get { return textboxText; }
set { SetProperty(ref textboxText,value); }
}

}

Thus when the textbox lose focus, the property textboxText is updated.

In MainWindow

public class MainWindow
{
public UserControl UserControlInstance = new UserControl();
public string textPropertyMainWindow;
public MainWindow()
{
UserControlInstance.TextBoxText.PropertyChanged += PropertyChangedHandler;
}

private void PropertyChangedHandler(object obj)
{
textPropertyMainWindow = UserControlInstance.TextBoxText;
}
}

Hope you got the point. Revert if any further help needed.

Reference a control from parent window in a custom user control

I ended up setting the PlacementTarget in the constructor of the window that the user control was being used in.
I put the following line in the constructor of the parent window:

ContactPopup3.ContactTooltip.PlacementTarget = PopupButton3;

This is not ideal (as it will need to be set in the code behind every time the user control is used), but it solved the problem.

WPF Get UserControl owner (the container element)

I came up with this solution, but post if you have a better one. Thanks!

DependencyObject ucParent = this.Parent;

while (!(ucParent is UserControl))
{
ucParent = LogicalTreeHelper.GetParent(ucParent);
}


Related Topics



Leave a reply



Submit