2013-01-16 35 views
0

綁定控制我有看法:WPF - 如何通過MVVM

<Grid> 
    <!--Some Stuff--> 
    <Control XXX="{Binding ButtomControl}"/> 
    <!--Some Stuff--> 
</Grid> 

我有VM:

public sealed class SelectionDialogV3VM : PropertyChanges 
{ 
    // Some Stuff 
    public Control ButtomControl 
    { 
     get{return _buttomControl;} 
     set 
     { 
      _buttomControl = value; 
      OnPropertyChanged("ButtomControl"); 
     } 
    } 
    // Some Stuff 
} 

我的目標是:在運行時更改我的主視圖裏面的一些觀點(ButtomControl) 。 但是,我不能做適當的綁定,因爲我不知道XXX屬性。

感謝

+0

A控制了ViewModel無關。 –

回答

2

嘗試是這樣的:

<ContentControl Content="{Binding ButtomControl}"/> 

但說實話,有Control型在你的視圖模型的屬性不是一個好兆頭:d

+0

感謝您的回答。你在VM中放置了哪個屬性?什麼,它實現了一些界面? – zzfima

+0

@zzfima這真的取決於你想要做什麼。 MVVM很難正確使用,並且需要時間來適應它。 –

+1

嗯,這不是非常棘手。但以正確的方式實施它需要很多紀律。只有幾條規則:1.控件,動畫,顏色等視覺元素只能在XAML或代碼背後觸及。可能會有一些例外,但在大多數情況下它可以工作。 2.業務邏輯在ViewModels中處理。 3.模型應儘可能簡單,不應包含任何業務特定的邏輯。 – DHN

1

使用ContentPresenter:

<ContentPresenter Content="{Binding ButtomControl}"/> 

無論如何,綁定到控件是很奇怪的!