下面是一个基于WVVM模式实现的详细设计代码示例:

首先,定义一个可绑定的NoNeedDetails类,其中包含一个ObservableCollection属性:

public class NoNeedDetails : INotifyPropertyChanged
{
    private ObservableCollection<string> _selectPackage;

    public ObservableCollection<string> SelectPackage
    {
        get { return _selectPackage; }
        set
        {
            if (_selectPackage != value)
            {
                _selectPackage = value;
                OnPropertyChanged(nameof(SelectPackage));
            }
        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

接下来,定义一个UserControl,并在其代码-behind中添加一个DependencyProperty,用于绑定NoNeedDetails的实例:

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty NoNeedDetailsProperty =
        DependencyProperty.Register("NoNeedDetails", typeof(NoNeedDetails), typeof(MyUserControl), new PropertyMetadata(null));

    public NoNeedDetails NoNeedDetails
    {
        get { return (NoNeedDetails)GetValue(NoNeedDetailsProperty); }
        set { SetValue(NoNeedDetailsProperty, value); }
    }

    public MyUserControl()
    {
        InitializeComponent();
    }
}

然后,在XAML中使用DataGrid和ComboBox来展示和绑定数据:

<UserControl x:Class="YourNamespace.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:YourNamespace"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d">
    <Grid>
        <DataGrid ItemsSource="{Binding NoNeedDetails.SelectPackage}">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding DataContext.NoNeedDetails.SelectPackage, RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

最后,在使用UserControl的地方,将NoNeedDetails实例绑定到UserControl的NoNeedDetails属性上:

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:MyUserControl NoNeedDetails="{Binding YourNoNeedDetailsProperty}"/>
    </Grid>
</Window>

以上代码示例演示了如何在WVVM模式下将NoNeedDetails类的ObservableCollection属性绑定到UserControl的DataGrid中的下拉框中。请根据实际情况进行修改和适配

UserControl的datagrid中下拉框绑定BindableCollectionNoNeedDetails中的ObservableCollectionstring SelectPackage值以WVVM模式实现请给出详细设计代码

原文地址: http://www.cveoy.top/t/topic/hXIL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录