xaml DataGridComboBoxColumn绑定 30
我有一个List<Device>用来保存设备列表,并且绑定到了DataGrid,已经可以显示了。但是我还有一列<DataGridComboBoxColumnHeader=...
我有一个List<Device>用来保存设备列表,并且绑定到了DataGrid,已经可以显示了。但是我还有一列
<DataGridComboBoxColumn Header="Option">这列希望根据设备类型决定下拉项
比如有的设备只能进行开关机操作,我希望只绑定“开机”,“关机”两项
有的设备可能除了开关机还要绑定其他的操作项
请问如何实现
效果类似这个
http://zhidao.baidu.com/link?url=AuOjB2q_nP3gqmprYV3kMu9UjMHUcmy0DZiVyJQDo2Kl0YEFwpyi-qqKfn1SxPaU9etJRNRD610_rG_YR40JJa 展开
<DataGridComboBoxColumn Header="Option">这列希望根据设备类型决定下拉项
比如有的设备只能进行开关机操作,我希望只绑定“开机”,“关机”两项
有的设备可能除了开关机还要绑定其他的操作项
请问如何实现
效果类似这个
http://zhidao.baidu.com/link?url=AuOjB2q_nP3gqmprYV3kMu9UjMHUcmy0DZiVyJQDo2Kl0YEFwpyi-qqKfn1SxPaU9etJRNRD610_rG_YR40JJa 展开
展开全部
您好,这样:
public partial class MainWindow : Window
{
public static List Projects = new List();
public static List Users = new List();
public static Hashtable Dutys = new Hashtable();
public static List WorkLoads = new List();
public static Hashtable Percents = new Hashtable();
public MainWindow()
{
Users.Add(new User("陈凤伟"));
Users.Add(new User("郝鹏飞"));
Users.Add(new User("李栋"));
Users.Add(new User("陈力平"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now,"负责人"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now,"参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Dutys.Add(0, "负责人");
Dutys.Add(1, "参与者");
Dutys.Add(2, "观察员");
Dutys.Add(3, "酱油");
WorkLoads.Add(10);
WorkLoads.Add(20);
WorkLoads.Add(40);
WorkLoads.Add(80);
Percents.Add(1, 20);
Percents.Add(2, 40);
Percents.Add(3, 60);
Percents.Add(4, 80);
InitializeComponent();
}
}
public class User
{
static int num = 0;
public int ID { get; set; }
public string Name { get; set; }
public User(string name)
{
ID = num++;
Name = name;
}
}
public class Project
{
static int num = 0;
public int ID { get; set; }
public string Name { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public string Duty { get; set; }
public double WorkLoad { get; set; }
public int Percent { get; set; }
public Project(string name, DateTime startDateTime, DateTime endDateTime,string duty)
{
ID = num++;
Name = name;
StartDateTime = startDateTime;
EndDateTime = endDateTime;
Duty = duty;
WorkLoad = 40;
Percent = 20;
}
}
我所定义的静态变量是这些,其中Projects为DataGrid需要绑定的数据,其余都是DataGrid中DataGridComboBoxColumn列需要绑定的数据。
public static List Projects = new List();
public static List Users = new List();
public static Hashtable Dutys = new Hashtable();
public static List WorkLoads = new List();
public static Hashtable Percents = new Hashtable();
OK,我们来看一看,那么前台XAML代码是怎样实现绑定的呐!
需要声明一下命名空间:
xmlns:local="clr-namespace:WpfApplication3"
绑定数据
< DataGrid ItemsSource="{x:Static local:MainWindow.Projects}" >
< DataGrid.Columns>
< DataGridTextColumn Header="编号" Binding="{Binding ID}"/>
< DataGridComboBoxColumn Header="姓名" ItemsSource="{x:Static local:MainWindow.Users}" DisplayMemberPath="Name" SelectedValuePath="Name" TextBinding="{Binding Path=Name}"/>
< DataGridTextColumn Header="起始日期" Binding="{Binding StartDateTime}" />
< DataGridTextColumn Header="截止日期" Binding="{Binding EndDateTime}" />
< DataGridComboBoxColumn Header="职责" ItemsSource="{x:Static local:MainWindow.Dutys}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Duty}">
< DataGridComboBoxColumn Header="参与小时数(h)" ItemsSource="{x:Static local:MainWindow.WorkLoads}" TextBinding="{Binding Path=WorkLoad}">
< DataGridComboBoxColumn Header="参与工作量(%)" ItemsSource="{x:Static local:MainWindow.Percents}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Percent}"> < /DataGridComboBoxColumn>
< /DataGrid.Columns>
< /DataGrid>
public partial class MainWindow : Window
{
public static List Projects = new List();
public static List Users = new List();
public static Hashtable Dutys = new Hashtable();
public static List WorkLoads = new List();
public static Hashtable Percents = new Hashtable();
public MainWindow()
{
Users.Add(new User("陈凤伟"));
Users.Add(new User("郝鹏飞"));
Users.Add(new User("李栋"));
Users.Add(new User("陈力平"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now,"负责人"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now,"参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Dutys.Add(0, "负责人");
Dutys.Add(1, "参与者");
Dutys.Add(2, "观察员");
Dutys.Add(3, "酱油");
WorkLoads.Add(10);
WorkLoads.Add(20);
WorkLoads.Add(40);
WorkLoads.Add(80);
Percents.Add(1, 20);
Percents.Add(2, 40);
Percents.Add(3, 60);
Percents.Add(4, 80);
InitializeComponent();
}
}
public class User
{
static int num = 0;
public int ID { get; set; }
public string Name { get; set; }
public User(string name)
{
ID = num++;
Name = name;
}
}
public class Project
{
static int num = 0;
public int ID { get; set; }
public string Name { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public string Duty { get; set; }
public double WorkLoad { get; set; }
public int Percent { get; set; }
public Project(string name, DateTime startDateTime, DateTime endDateTime,string duty)
{
ID = num++;
Name = name;
StartDateTime = startDateTime;
EndDateTime = endDateTime;
Duty = duty;
WorkLoad = 40;
Percent = 20;
}
}
我所定义的静态变量是这些,其中Projects为DataGrid需要绑定的数据,其余都是DataGrid中DataGridComboBoxColumn列需要绑定的数据。
public static List Projects = new List();
public static List Users = new List();
public static Hashtable Dutys = new Hashtable();
public static List WorkLoads = new List();
public static Hashtable Percents = new Hashtable();
OK,我们来看一看,那么前台XAML代码是怎样实现绑定的呐!
需要声明一下命名空间:
xmlns:local="clr-namespace:WpfApplication3"
绑定数据
< DataGrid ItemsSource="{x:Static local:MainWindow.Projects}" >
< DataGrid.Columns>
< DataGridTextColumn Header="编号" Binding="{Binding ID}"/>
< DataGridComboBoxColumn Header="姓名" ItemsSource="{x:Static local:MainWindow.Users}" DisplayMemberPath="Name" SelectedValuePath="Name" TextBinding="{Binding Path=Name}"/>
< DataGridTextColumn Header="起始日期" Binding="{Binding StartDateTime}" />
< DataGridTextColumn Header="截止日期" Binding="{Binding EndDateTime}" />
< DataGridComboBoxColumn Header="职责" ItemsSource="{x:Static local:MainWindow.Dutys}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Duty}">
< DataGridComboBoxColumn Header="参与小时数(h)" ItemsSource="{x:Static local:MainWindow.WorkLoads}" TextBinding="{Binding Path=WorkLoad}">
< DataGridComboBoxColumn Header="参与工作量(%)" ItemsSource="{x:Static local:MainWindow.Percents}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Percent}"> < /DataGridComboBoxColumn>
< /DataGrid.Columns>
< /DataGrid>
追问
可能我是新手,没看明白
我不知道哪段代码实现了我要求的功能,你这段代码只是绑定了后台数据而已
拿你这段代码来说,我希望的功能是根据人名,职责里面显示的选项有区别。比如陈凤伟可以选择所有4个职责,而郝鹏飞只能选择观察者和酱油两个职责
另外,我的Device类是没有Option这个成员的
如果我说的哪里不对,请指教谢谢~
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询