[英]WPF Usercontrol, Hosting dynamic content for reuse of UserControl. MVVM
我會盡力弄清自己要完成的工作,並願意就實現我期望的結果的其他方法提出建議。
1000英尺的視圖。
我有一個要在應用程序的每個屏幕中重用的UserControl
。 此控件更多是帶有圖標( bindable
),動態( bindable
)標簽的模板外觀。
UserControl.xaml (CardView.xaml)
<Border BorderBrush="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
<StackPanel Orientation="Vertical" Style="{StaticResource CardStyle}">
<StackPanel>
<Border Style="{StaticResource MyBorderStyle}">
<Label Content="{Binding Path=CardTitle, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</Border>
</StackPanel>
<Label Style="{StaticResource LabelIcon}">
<Path Fill="#FF000000" HorizontalAlignment="Center"
Stretch="UniformToFill" Data="{Binding Path=VectorString, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
// Dynamic Content Here.
// Any kind of XAML content for the consumer of the control.
// not in C# but I want to host the control and put controls in
// here that I can bind to in XMAL by parents view model.
</StackPanel>
</Border>
消費者代碼 (customer.xaml)
<local:CardView CardTitle="Test" VectorString="F1 M" BorderColor="#FF0088">
// Here's where I want to put dynamic XAML content.
// Want to host anything and bind to it using the consumers View Model.
// Example
<Button Content="{Binding SomeText}" />
<StackPanel>
<Button>..... variable content but bindable
</StackPanel>
</local:CardView>
因此,總而言之,我有一個用戶控件,該控件要在多個地方使用,並且主體中具有可變內容。 變量內容將在使用者XAML中標記出來。
我已經搜尋了一些建議,但似乎與模型不符
使用內容模板。 我本來打算但是如何綁定到內容模板中的控件?
內容演示者。 如何綁定到消費者視圖模型?
通過使用ContentControl
這很容易做到。 只需將其插入您的CardView.xaml中即可:
<Border BorderBrush="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
<StackPanel Orientation="Vertical" Style="{StaticResource CardStyle}">
<StackPanel>
<Border Style="{StaticResource MyBorderStyle}">
<Label Content="{Binding Path=CardTitle, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</Border>
</StackPanel>
<Label Style="{StaticResource LabelIcon}">
<Path Fill="#FF000000" HorizontalAlignment="Center"
Stretch="UniformToFill" Data="{Binding Path=VectorString, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
// Dynamic Content Here.
<ContentControl Content={Binding CustomContent} />
</StackPanel>
然后在ViewModel中添加類型為object的屬性'CustomContent',其中包含Customer
的實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.