如何在 DataGridComboBoxColumn 中设置选择?
How can I set a selection in a DataGridComboBoxColumn?
如何以编程方式设置 DataGridComboBoxColumn 项目选择?
我想用数据填充我的列表,comboboxitem 应该设置列表中的选定项目。
但我找不到任何方法来实现它。
在此示例中,选择应为 "Forward"
XAML:
<DataGrid ItemsSource="{Binding }" x:Name="dgSFD" AlternatingRowBackground="BlanchedAlmond" SelectionChanged="dgSFD_SelectionChanged" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=sID}" Header="Step ID"/>
<DataGridComboBoxColumn x:Name="cbServo" Header="Servo" SelectedItemBinding="{Binding Servo, Mode =TwoWay}" SelectedValuePath="sFunctionName ,Mode=TwoWay" DisplayMemberPath="sFunctionName"/>
</DataGrid>
Class
public class Step
{
public string sID { get; set; }
public Servo serServo { get; set; }
}
public class Servo
{
public string sFunction { get; set; }
public string sServo { get; set; }
}
C#
public static List<Step> listStep { get; set; }
public static List<Servo> listServo { get; set; }
public MainWindow()
{
InitializeComponent();
}
public void loadList()
{
dgSFD.ItemsSource = listStep;
cbServo.ItemsSource = listServo;
}
public void testfill()
{
Servo newServo = new Servo();
newServo.sFunctionName = "Forward";
newServo.sServo = "Left";
listServo.Add(newServo);
Step newStep = new Step();
newStep.serServo = newServo;
newStep.sID = "1";
listStep.Add(newStep);
}
将 SelectedItemBinding
属性 绑定到 Step
对象的 serServo
属性:
<DataGridComboBoxColumn x:Name="cbServo" Header="Servo" SelectedItemBinding="{Binding serServo, Mode=TwoWay}" DisplayMemberPath="sFunction"/>
如何以编程方式设置 DataGridComboBoxColumn 项目选择?
我想用数据填充我的列表,comboboxitem 应该设置列表中的选定项目。
但我找不到任何方法来实现它。
在此示例中,选择应为 "Forward"
XAML:
<DataGrid ItemsSource="{Binding }" x:Name="dgSFD" AlternatingRowBackground="BlanchedAlmond" SelectionChanged="dgSFD_SelectionChanged" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=sID}" Header="Step ID"/>
<DataGridComboBoxColumn x:Name="cbServo" Header="Servo" SelectedItemBinding="{Binding Servo, Mode =TwoWay}" SelectedValuePath="sFunctionName ,Mode=TwoWay" DisplayMemberPath="sFunctionName"/>
</DataGrid>
Class
public class Step
{
public string sID { get; set; }
public Servo serServo { get; set; }
}
public class Servo
{
public string sFunction { get; set; }
public string sServo { get; set; }
}
C#
public static List<Step> listStep { get; set; }
public static List<Servo> listServo { get; set; }
public MainWindow()
{
InitializeComponent();
}
public void loadList()
{
dgSFD.ItemsSource = listStep;
cbServo.ItemsSource = listServo;
}
public void testfill()
{
Servo newServo = new Servo();
newServo.sFunctionName = "Forward";
newServo.sServo = "Left";
listServo.Add(newServo);
Step newStep = new Step();
newStep.serServo = newServo;
newStep.sID = "1";
listStep.Add(newStep);
}
将 SelectedItemBinding
属性 绑定到 Step
对象的 serServo
属性:
<DataGridComboBoxColumn x:Name="cbServo" Header="Servo" SelectedItemBinding="{Binding serServo, Mode=TwoWay}" DisplayMemberPath="sFunction"/>