我会尽量清楚我想要完成的事情,并愿意接受有关其他方法的建议,以达到我想要的结果。
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中标记。
一些建议我挖了搜索,但似乎不适合模型
使用内容模板。我打算但是你如何绑定到内容模板中的控件?
内容演示者。如何绑定到消费者视图模型?
答案 0 :(得分:0)
使用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
的实例。