[英]How to bind a Grid to a UserControl Property in XAML?
I have a UserControl (let's call it "CustomControl") that acts as a simple toolbar for a grid. 我有一个UserControl(简称为“ CustomControl”),它充当网格的简单工具栏。 It contains only a button to open/close (it just changes it's visibility property) a Grid.
它仅包含一个用于打开/关闭(仅更改其可见性属性)网格的按钮。
Like this: 像这样:
<UserControl ....>
...
<Grid>
<Button x:Name="btChangeState" Content="Change State" />
</Grid>
....
</UserControl>
And I have declared the DependencyProperty of a Grid like the following: 我已经声明了如下所示的Grid的DependencyProperty:
public Grid MyContent
{
get { return (Grid)GetValue(MyContentProperty); }
set { SetValue(MyContentProperty, value); }
}
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("MyContent", typeof(Grid), typeof(MyControl), new PropertyMetadata(null));
And my user control button has a simple handler to change the "MyContent" Grid Visible or collapsed: 我的用户控件按钮具有一个简单的处理程序,可以更改“可见的”或折叠的“ MyContent”网格:
if (MyContent.Visibility == Windows.UI.Xaml.Visibility.Visible)
MyContent.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
else
MyContent.Visibility = Windows.UI.Xaml.Visibility.Visible;
On my ViewPage, I am able to wire up the theUserControl.MyContent to theGrid in the constructor of the Page, but how can I do that in the XAML? 在ViewPage上,我可以在Page的构造函数中将theUserControl.MyContent连接到theGrid,但是如何在XAML中实现呢? I would like to do something like this:
我想做这样的事情:
<Page .....>
<StackPanel Orientation="Vertical">
<cs:CustomControl x:Name="theUserControl" MyContent="{Binding theGrid}" />
<Grid x:Name="theGrid" Height="200" Width="200" Background="Red" />
</StackPanel>
</Page>
Is it possible? 可能吗?
您需要使用ElementName
:
<cs:CustomControl x:Name="theUserControl" MyContent="{Binding ElementName=theGrid}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.