【问题标题】:Best practice adding UserControl to parent control via binding通过绑定将 UserControl 添加到父控件的最佳实践
【发布时间】:2016-05-27 18:15:07
【问题描述】:

我有一个ObservableCollection<ControlMachine>,其中每个ControlMachine 是一个UserControl,用于显示有关一台计算机的信息。

    public static ObservableCollection<ControlMachine> ListControleMachine =
                        new ObservableCollection<ControlMachine>();

我尝试使用绑定显示集合

   <Grid ForceCursor="True" x:Name="grid2" OpacityMask="{x:Null}" Margin="10,38,10,10" Background="#FFF5F6F6">
       <ItemsControl ItemsSource="{Binding ListControleMachine}">
           <ItemsControl.ItemTemplate>
               <DataTemplate DataType="{x:Type Brush}">
                   <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="100" Canvas.Left="100" Fill="blue"/>
               </DataTemplate>
           </ItemsControl.ItemTemplate>
       </ItemsControl>
   </Grid>

我的用户控件没有显示在Grid 中,带有此代码。怎么了?

问题:将此类 ControlMachine 置于其他设备之下的最佳做法是什么?

那些 ControlMachine 可以关闭和展开,我找不到 clean 方法来更新任何情况下的用户控件位置。

附件:您认为在这种情况下使用UserControl 是一种好习惯还是我应该使用TemplateStackPanel

你将如何实现它?

【问题讨论】:

    标签: c# wpf binding user-controls


    【解决方案1】:

    此解决方案的最佳实践是使用 ViewModel 而不是 UserControl 并将其绑定到您的网格中。 您可以在 ViewModel 中使用 DependencyProperty 和 INotifyPropertyChanged 进行两种方式绑定。 所以你不会警告处置...。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2019-03-10
      相关资源
      最近更新 更多