gpt4 book ai didi

wpf - 绑定(bind) Observable 集合

转载 作者:行者123 更新时间:2023-12-03 10:20:18 25 4
gpt4 key购买 nike

我在主窗口中有一个集合,我想在用户控件的网格上显示它,
什么是正确的 MVVM 方法?

我在 MainWindow 中完成了 observableCollection 并将其绑定(bind)到用户控件中的 observableCollection。并且在用户控件中,网格有界
集合。

它不起作用:(

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<string> MyNames
{
get { return (ObservableCollection<string>)GetValue(MyNamesProperty); }
set { SetValue(MyNamesProperty, value); }
}

// Using a DependencyProperty as the backing store for Names. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyNamesProperty =
DependencyProperty.Register("MyNames", typeof(ObservableCollection<string>), typeof(MainWindow), new UIPropertyMetadata(null));

public MainWindow()
{
MyNames = new ObservableCollection<string>() { "Jonh", "Mary" };
this.InitializeComponent();
DataContext = this;
}
}

主窗口 XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
x:Class="WpfApplication3.MainWindow"
x:Name="Window"
Title="MainWindow"
UseLayoutRounding="True"
Width="640" Height="480">
<Grid>
<local:NamesControl Names="{Binding MyNames}"></local:NamesControl>
</Grid>

用户控制:
  public partial class NamesControl : UserControl
{
public ObservableCollection<string> Names
{
get { return (ObservableCollection<string>)GetValue(NamesProperty); }
set { SetValue(NamesProperty, value); }
}

// Using a DependencyProperty as the backing store for Names. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NamesProperty =
DependencyProperty.Register("Names", typeof(ObservableCollection<string>), typeof(NamesControl), new UIPropertyMetadata(null));



public NamesControl()
{
InitializeComponent();
DataContext = this;
}
}

用户控件 XAML:
<UserControl x:Class="WpfApplication3.NamesControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ItemsControl ItemsSource="{Binding Names}"/>
</Grid>

最佳答案

做到这一点的“正确方法”将需要三件事:

  • 主窗口
  • 用户控制
  • 查看型号

  • 在 ViewModel 中,您要创建 ObservableCollection 并将其设置为 ViewModel 上的属性,如下所示:
    public class MyListViewModel
    {
    public MyViewModel()
    {
    MyObjects = new ObservableCollection<MyObject>();
    // Add items to collection
    }

    public ObservableCollection<MyObject> MyObjects{ get; set; }
    }

    然后,在您的 UserControl 的 Initialize 方法中,您要实例化 ViewModel 并将其附加到该 UserControl 的 DataContext:
    public AgentListView()
    {
    InitializeComponent();
    DataContext = new MyViewModel();
    }

    注意:如果您使用 IoC 容器为您处理依赖关系解决方案,这会容易得多,但为了简单起见,我在这里跳过它。

    在您的 UserControl 中,您要为 UserControl 指定 DataContext,然后为 DataGrid 和列指定单独的绑定(bind):
    <UserControl x:Class="UserControls.Views.AgentDataGridView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:utility="clr-namespace:UserControls.Utility"
    mc:Ignorable="d"
    d:DataContext="{Binding}">
    <GroupBox Header="Agent States" Height="auto" Margin="0,5,0,0" Name="_groupBox" VerticalAlignment="Top" BorderBrush="DarkSlateBlue">
    <Grid Name="_grid" ShowGridLines="True" Margin="5" >
    <toolkit:DataGrid
    ItemsSource="{Binding MyObjects, Mode=OneWay}">
    <toolkit:DataGrid.Columns>
    <toolkit:DataGridTextColumn Binding="{Binding StateAndJobDescription, Mode=OneWay, NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" Header="State" Width="100" IsReadOnly="True" />
    <toolkit:DataGridTextColumn Binding="{Binding SubStateDescription, Mode=OneWay, NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" Header="City" Width="120" IsReadOnly="True" />
    </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>
    </Grid>
    </GroupBox>
    </UserControl>

    从这里,您只需将 UserControl 添加到您的 MainWindow。

    关于wpf - 绑定(bind) Observable 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684546/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com