好的,從您的意見我收集,你希望每個上下文有自己的XAML文件,但它是指一些常見的模板。那麼你可以如下:
設置通用模板,例如在CommonControl中。XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Fill="Yellow" />
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Text="This is some fixed text"/>
<ComboBox Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<StackPanel Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox>My first checkbox</CheckBox>
<CheckBox>My second checkbox</CheckBox>
</StackPanel>
<ContentControl Grid.Row="1" Grid.Column="1" Content="{Binding Path=SpecificPart1, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
<Ellipse Grid.Row="1" Grid.Column="2" Fill="Blue"/>
</Grid>
這具有在形成網格的所有位置的公共UI的各種UI元素,除了網格行1,列1(下部中心窗格)中,其中上下文特定UI將被顯示。
代碼
現在的背後,CommonControl.xaml.cs,你設置的依賴對象,使上下文中的具體UI被送入模板:
public object SpecificPart1
{
get { return this.GetValue(SpecificPart1Property); }
set { this.SetValue(SpecificPart1Property, value); }
}
public static readonly DependencyProperty SpecificPart1Property = DependencyProperty.Register(
"SpecificPart1", typeof(object), typeof(CommonControl), new PropertyMetadata(false));
如果有一個以上的位置您希望放置特定於上下文的用戶界面,然後您可以在此處爲其他部分添加另一個依賴項屬性。
然後,您可以設置許多其他XAML文件,只要您喜歡,這些文件都會引用此內容並插入它們自己的UI以進入不同的部分。下面是一個例子,其中提供了一些綠色的斜體文字。在MySpecificControl.xaml中:
<my:CommonControl>
<my:CommonControl.SpecificPart1>
<TextBlock Foreground="DarkGreen" FontStyle="Italic" FontSize="14">This is my context specific UI</TextBlock>
</my:CommonControl.SpecificPart1>
</my:CommonControl>
對不起,但我不明白你的解決方案。它的共同控制在哪裏?我在尋找的是一種方式來編寫所有我的視圖所使用的所有控件(可能在一個模板中),然後爲每個用例指定多個視圖,其中我只指定要添加的基本模板。有沒有簡單的方法來實現這一目標? – Filippo 2012-03-01 15:24:06
也許我誤解了你的問題。你能更清楚一點嗎?因爲就目前而言,你對我的回答感到困惑的是我對你的評論感到困惑。 – 2012-03-01 17:36:19
在MVVM場景中,我有和想要閱讀的視圖,一個用於編輯,一個用於創建,另一個用於創建特殊情況,ecc。所有這些觀點都分享了他們大部分的組成部分,只有很小的差異:我可以在哪裏放置通用部分?我不想要一個適應CRUD操作的獨特xaml文件,我想要更多的xaml文件,它以某種方式從另一個xaml文件「繼承」公共組件。可能是模板是正確的答案,但我需要遵循一些示例。 – Filippo 2012-03-02 08:43:32