2013-08-27 127 views
15

我有用戶控制:WPF用戶控制數據綁定到用戶控件屬性

XAML

<UserControl x:Class="controlmaker.checkButton" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="114" d:DesignWidth="221"> 
<Grid Background="Aqua" > 
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="58,24,0,0" Name="checkBox1" VerticalAlignment="Top" /> 
    <Button Content="{Binding buttText}" Height="23" HorizontalAlignment="Left" Margin="58,57,0,0" Name="button1" VerticalAlignment="Top" Width="75" /> 
</Grid> 
</UserControl> 

代碼後面

public partial class checkButton : UserControl 
{ 
    public checkButton() 
    { 
     InitializeComponent(); 
    } 


    public static readonly DependencyProperty buttTextProperty = 
DependencyProperty.Register("buttText", typeof(String), 
typeof(checkButton), new FrameworkPropertyMetadata(string.Empty)); 

    public String buttText 
    { 
     get { return GetValue(buttTextProperty).ToString(); } 
     set { SetValue(buttTextProperty, value); } 
    } 


} 

和主窗口XAML

<Window x:Class="controlmaker.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:controlmaker"> 
<Grid> 
    <my:checkButton buttText="aka" HorizontalAlignment="Left" Margin="145,115,0,0" x:Name="checkButton1" VerticalAlignment="Top" Height="133" Width="250" /> 
</Grid> 
</Window> 

我想要綁定用戶控件rty在窗口xaml中並將其綁定到用戶控件中以便按鈕內容屬性。怎麼做?

+21

的'butt'前綴把靜態文本不過是最好的。 #facepalm – CodeAngry

+0

@CodeAngry同意:D – NirmalL

回答

32

您可以嘗試用戶控件內部的元素綁定。只要給一個名稱,用戶控件和綁定屬性:

<UserControl x:Class="controlmaker.checkButton" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="114" d:DesignWidth="221" 
     x:Name="MyUserControl"> 
<Grid Background="Aqua" > 
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" 
      Margin="58,24,0,0" Name="checkBox1" VerticalAlignment="Top" /> 
    <Button Content="{Binding Path=buttText, ElementName=MyUserControl}" 
      Height="23" HorizontalAlignment="Left" Margin="58,57,0,0" 
      Name="button1" VerticalAlignment="Top" Width="75" /> 
</Grid> 
</UserControl> 

然後你就可以綁定或用戶控制使用地點

<my:checkButton buttText="aka" /> 

<my:checkButton buttText="{Binding SomeProperty}" /> 
+9

任何人更感興趣如何使它更正確,我強烈建議[鏈接](http://www.scottlogic.com/blog/2012/02/06/a-simple-pattern- for-creating-re-useable-usercontrols-in-wpf-silverlight.html) – newman

+0

如何指定VM用戶控件的類型應視爲參考數據?想象一下,虛擬機不在同一個命名空間等 – stenly

+1

問題是 - 在自己的xaml中綁定用戶控件屬性,當前示例不使用MVVM模式,如果您需要在用戶控件中使用MVVM並綁定視圖模型,那麼您必須將用戶控件的DataContext設置爲View Model實例,或者如果需要類型重新考慮,請考慮DataTemplate:http://stackoverflow.com/questions/3149117/how-to-automatically-use-a-datatemplate-based-on-contentcontrols-current- conten –