2012-02-27 26 views
3

我有以下場景:我正在創建一個紙牌遊戲,並且作爲它的一部分,我想創建一個UserControl以使界面編程更容易,更好。我創建了以下的用戶控件:如何實現元素後面的代碼的DataBinding?

的.cs

public partial class ChimeraUserControl : UserControl 
{ 
    private ChimeraViewModel Chimera { get; set; } 

    public ChimeraUserControl(Chimera chimera) 
    { 
     this.Chimera = new ChimeraViewModel(chimera); 
     InitializeComponent(); 
    } 
} 

我希望能夠做兩件事情:使用該用戶控件時,才能夠通過結合發送Chimera,也,使所有的文本和其他元素綁定到這個Chimera。我搜查了很多,但沒有找到讓我滿意的東西。

你們認爲什麼?

我已經嘗試過閱讀:

http://dev-for-fun.blogspot.com/2008/06/wpf-example-create-usercontrol-and.html

Binding from View-Model to View-Model of a child User Control in Silverlight? 2 sources - 1 target

和許多其他的網頁,但沒有似乎很直接,和黑客的代碼我沒有找到我的問題的解決方案。

+0

從upvotes我得到我想這是一個真正的問題缺乏文件綁定。 – 2012-02-27 04:27:38

+0

將xaml或代碼添加到您試圖執行所需內容的地方。它會更精確地顯示問題。 – 2012-02-27 09:32:01

+0

後面的代碼就在這裏。稍後我會添加一塊XAML,儘管這不是IMO必需的,因爲我無法在那裏做任何事情。 – 2012-02-27 13:07:30

回答

4

首先,讓Chimera一個Dependency Property所以它可以參與綁定系統

public static readonly DependencyProperty ChimeraProperty = 
    DependencyProperty.Register("Chimera ", typeof(ChimeraViewModel), 
    typeof(ChimeraUserControl), new FrameworkPropertyMetadata(null)); 

public ChimeraViewModel Chimera 
{ 
    get { return (ChimeraViewModel)GetValue(ChimeraProperty); } 
    set { SetValue(ChimeraProperty, value); } 
} 

其次,你可以通過一個RelativeSourceElementName結合

<UserControl x:Name="ChimeraViewRoot" ... > 

    <StackPanel> 
     <!-- ElementName Binding --> 
     <TextBlock Text="{Binding ElementName=ChimeraViewRoot, Path=Chimeria.Name}" /> 

     <!-- RelativeSource Binding --> 
     <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ChimeraView}}, Path=Chimeria.Name}" /> 

    </StackPanel> 
</UserControl> 
引用您的 Chimeria財產

你也可以設置0控件你UserControlChimera屬性裏面,讓您綁定語法清潔

<UserControl x:Name="ChimeraViewRoot" ... > 
    <StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ChimeraView}}, Path=Chimeria}" > 

     <TextBlock Text="{Binding Name}" /> 
     <TextBlock Text="{Binding Description}" /> 

    </StackPanel> 
</UserControl> 

我經常因爲這個值應該是從任何使用UserControl通過不建議你UserControl定義UserControl.DataContext。設置在UserControl內可能會導致混淆,當您試圖找出爲什麼某個特定的UserControl不適用於預期的DataContext時。


個人,當我創建一個應該去一個特定UserControl一個ViewModel,我希望設置DataTemplate的應用程序,這樣的所有實例我ViewModel拿得出與我的自定義UserControl。這意味着,我假設UserControl.DataContext將永遠是一個特定ViewModel

<DataTemplate DataType="{x:Type local:ChimeriaViewModel}"> 
    <local:ChimeriaView /> <!-- DataContext will always be ChimeriaViewModel --> 
</DataTemplate> 

這將使用ChimeriaView隱含每當視覺樹遇到ChimeriaViewMmodel類型的對象。

例如,下面將呈現一個充滿ChimeriaView對象

<ItemsControl ItemsSource="{Binding MyListOfChimeriaViewModels}" /> 

或顯示一個對象,我通常會使用的東西就像一個ContentControl

<!-- Will get drawn using ChimeriaView due to DataTemplate defined above --> 
<ContentControl Content="{Binding MyChimeriaViewModelProperty}" /> 

此外,通過了解StackPanelChimeriaView將成爲ChimeriaViewModel類型的對象,我會擺脫DependencyProperty

<UserControl> 
    <StackPanel> 
     <!-- I know the DataContext is ChimeriaViewModel --> 
     <TextBlock Text="{Binding Name}" /> 
     <TextBlock Text="{Binding Description}" /> 

    </StackPanel> 
</UserControl> 
+0

非常感謝!現在,這是直截了當的! – 2012-02-27 15:18:23

+0

問題:我定義了一個DataTemplate而不是UserControl,而不是? – 2012-03-02 02:14:04

+0

@Bruno我根據自己的需要使用'DataTemplates'和'UserControls'。如果在它自己的控件中對元素進行分組是有意義的,那麼我將使用一個'UserControl'。如果它很簡單,或者範圍較小,我將使用'DataTemplate'。此外,將「DataTemplate」放置在合理的位置。如果它定義了一個應用程序範圍的模板,則將它放在Application.Resources中。如果它是用戶控制模板,請將它放在UserControl.Resources中。 – Rachel 2012-03-02 03:24:30

相關問題