【问题标题】:WPF Usercontrol, Hosting dynamic content for reuse of UserControl. MVVMWPF 用户控件,托管动态内容以供用户控件重用。 MVVM
【发布时间】:2018-01-16 05:26:43
【问题描述】:

我会尽量清楚自己想要完成的工作,并愿意接受有关其他方法以实现我想要的结果的建议。

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 中进行标记。

我已经挖掘出一些建议,但似乎不适合模型

  1. 使用内容模板。我本来打算,但你如何绑定到内容模板中的控件?

  2. 内容演示者。如何绑定到消费者视图模型?

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    这很容易通过使用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 中添加一个类型为对象的属性“CustomContent”,其中包含您的Customer 的实例。

    【讨论】:

    • 我把它放在我的代码中,但它不起作用。当您说“在您的 ViewModel 中添加一个属性‘自定义内容’时,您的意思是在 CardView.cs 文件中注册依赖项属性还是在 CardViewViewModel.cs 文件中?试图在 XMAL 标记中放置一个按钮,但整个内容local:CardView 现在只有一个按钮,我只能有一个按钮
    • 我需要能够在消费者代码中做到这一点 。将任何内容放入标记中。使用您向我展示的代码,除非我缺少基本知识,否则我无法做到这一点。
    • 我希望我能澄清这一点。在 CardView.xaml 中,添加 Content 绑定到 CardViewViewmodel.cs 中的 CustomContent 属性。然后,您将 Customer 的一个实例放入 CustomContent 属性中。如果您现在想要切换到应用程序的另一个屏幕,则将该屏幕的一个实例放在 CustomContent 属性中。
    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多