I have MainWindow , MenuControl , EditorControl in my WPF App.
When I click the "Get Height" Button
in MenuControl
, I want to get the StackPanel
's Height
in EditorControl
.
How can I get the StackPanel
object in EditorControl
?
MenuControl.xaml:
<Button Content="Get Height" Click="GetStackPanelHeight_Click"/>
EditorControl.xaml:
<StackPanel x:Name="xxxx">
<Label Context="test">
</StackPanel>
MenuControl.xaml.cs:
public void GetStackPanelHeight_Click(object sender, RoutedEventArgs e)
{
var tmp = xxxx.ActualHeight;
}
can not access xxxx.AcutualHeight.
If I wrote below,tmp=0
var editorControl = new EditorControl();
var tmp = editorControl.xxxx.ActualHeight;
How can I do it?
One of the fastest solutions is: In EditorControl
you can get the value of the StackPanel
's ActualHeight
.
For example: Pass the StackPanel
's ActualHeight
value into a DependencyProperty
inside the EditorControl
. When the Button
in MenuControl
is being pushed, read the value of that dependency property.
You can do it like this :
public class FirstUserControl
{
public delegate void PropertyChangedHandler(object obj);
public static event PropertyChangedHandler StackPanelHeightChanged = delegate { };
public void GetStackPanelHeight_Click(object sender, RoutedEventArgs e)
{
//Example: here I fire the function in the second UC
var tmp = xxxx.ActualHeight;
StackPanelHeightChanged(tmp);
}
}
-
public class SecondUserControl
{
public SecondUserControl()
{
FirstUserControl.StackPanelHeightChanged+= OnStackPanelHeightChanged;
}
public void OnStackPanelHeightChanged(object obj)
{
//here obj is the Height of the StackPanel
}
//....
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.