我遇到了wpf datagrid组合框的问题。我有以下代码,其中的项目没有在组合框中填充,请帮助我,如果任何人在wpf工具包datagrid(不在Infragistics)中完成此操作,还让我知道如何使该组合框列为可编辑的?
<DataGrid Name="dataGridResultsAdded" AutoGenerateColumns="False" FontWeight="Normal" IsReadOnly="True" ItemsSource="{Binding UserResults,Mode=Default}" SelectedIndex="{Binding SelectedIndexUserResults}" SelectionMode="Single" Margin="0,0,0,0" Height="178" GridLinesVisibility="None">
<DataGrid.Columns>
<DataGridTextColumn Header="RFC ID" Binding="{Binding RFCID}"></DataGridTextColumn>
<DataGridTextColumn Header="RFC Title" Binding="{Binding RFCTitle}"></DataGridTextColumn>
<DataGridTextColumn Header="RFC Revision" Binding="{Binding RFCRevision}"></DataGridTextColumn>
<DataGridTextColumn Header="Trigger Association" Binding="{Binding TriggerAssociation}"></DataGridTextColumn>
<DataGridTemplateColumn Header="OrderStatus1" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox IsEditable="True" ItemsSource="{Binding Path=DataContext.OrderStatus,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" SelectedItem="{Binding Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridComboBoxColumn IsReadOnly="False" Header="OrderStatus" SelectedItemBinding="{Binding Status,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding OrderStatus, Mode=TwoWay}" />
<DataGridTextColumn Header="Status" Binding="{Binding TriggerStatus}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
private List orderStatus = default(List);
public List<string> OrderStatus
{
get
{
if (orderStatus == null)
{
orderStatus = new List<string>();
orderStatus.Add("None");
orderStatus.Add("New");
orderStatus.Add("Processing");
orderStatus.Add("Shipped");
}
return orderStatus;
}
set
{
orderStatus = value;
NotifyPropertyChanged("OrderStatus");
}
}
` public DataTable dtUserResults = default(DataTable);
public DataTable UserResults
{
get
{
return dtUserResults;
}
set
{
dtUserResults = value;
NotifyPropertyChanged("UserResults");
}
}
`查看下面的型号代码
public void AddExecute()
{
InfoHandler.LogInfo("Entering AddExecute");
try
{
DataTable dtUserRes = new DataTable();
DataColumn dColumn = default(DataColumn);
DataRow dRow = default(DataRow);
DataRow dRowSelected = dtSearchResults.NewRow();
dRowSelected = dtSearchResults.Rows[intSelectedIndexSearchRes];
if (dtSearchResults.Rows.Count > 0 && IsRFCAlreadyAdded(dRowSelected))
{
dColumn = new DataColumn("RFCID", Type.GetType("System.String"));
dtUserRes.Columns.Add(dColumn);
dColumn = new DataColumn("RFCTitle", Type.GetType("System.String"));
dtUserRes.Columns.Add(dColumn);
dColumn = new DataColumn("RFCRevision", Type.GetType("System.String"));
dtUserRes.Columns.Add(dColumn);
dColumn = new DataColumn("TriggerAssociation", Type.GetType("System.String"));
dtUserRes.Columns.Add(dColumn);
dColumn = new DataColumn("TriggerStatus", Type.GetType("System.String"));
dtUserRes.Columns.Add(dColumn);
//setting RFCID as primary key for the datatable UserResults
dtUserRes.PrimaryKey = new DataColumn[] { dtUserRes.Columns[0] };
dRow = dtUserRes.NewRow();
dRow["RFCID"] = dRowSelected["RFCID"];
dRow["RFCTitle"] = dRowSelected["RFCTitle"];
dRow["RFCRevision"] = dRowSelected["RFCRevision"];
dRow["TriggerAssociation"] = "Manual";
dRow["TriggerStatus"] = "N";
dtUserRes.Rows.Add(dRow);
if (UserResults == null)
{
UserResults = new DataTable();
}
UserResults.Merge(dtUserRes, true);
}
}
catch (Exception objEx)
{
ErrorHandler.ShowErrorMessage(strErrorCaption, "Error occured while Adding RFC from Search results to Execution List", objEx.Message);
}
InfoHandler.LogInfo("Exiting AddExecute");
}
`
由于 NallsKarthi
答案 0 :(得分:2)
这是正常的,你不是要求ComboBox
填充许多值。
只要我理解你的问题,你就有了
<ComboBox ItemsSource="{Binding Path=OrderStatus, Mode=TwoWay}" SelectedItem="{Binding Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
对象OrderStatus
的类型是什么?
而且,OrderStatus位于何处?
我想说的是:
<ComboBox ItemsSource="{Binding Path=DataContext.OrderStatus,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
(如果您在Window中,请将AncestorType更改为Window)
可以肯定的是,我需要更多代码,尤其是:ItemsSource的声明以及OrderStatus
的声明