0
我有一個UserControl(我們稱之爲「CustomControl」),它充當網格的簡單工具欄。它只包含一個打開/關閉按鈕(它只是改變它的可見性屬性)一個網格。如何將網格綁定到XAML中的UserControl屬性?
像這樣:
<UserControl ....>
...
<Grid>
<Button x:Name="btChangeState" Content="Change State" />
</Grid>
....
</UserControl>
而且我已經宣佈像網格的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));
而且我的用戶控制按鈕有一個簡單的處理程序,以改變「爲myContent」網格可見或摺疊:
if (MyContent.Visibility == Windows.UI.Xaml.Visibility.Visible)
MyContent.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
else
MyContent.Visibility = Windows.UI.Xaml.Visibility.Visible;
在我的ViewPage上,我能夠連接用戶C ontrol.MyContent到頁面的構造函數中的網格,但我怎麼能在XAML中做到這一點?我想這樣做:
<Page .....>
<StackPanel Orientation="Vertical">
<cs:CustomControl x:Name="theUserControl" MyContent="{Binding theGrid}" />
<Grid x:Name="theGrid" Height="200" Width="200" Background="Red" />
</StackPanel>
</Page>
這可能嗎?
謝謝!它完全按照我的意願工作! :) – dijeferson