wpf数据绑定xml到datagrid组合框?

时间:2017-07-06 13:22:20

标签: c# xml wpf combobox datagrid

我有一个xml文件:

<NewDataSet>
     <Communications>
         <ModelNumber>0x01</ModelNumber>
         <ParamName>BaudRate</ParamName>
         <ParamValue>19200</ParamValue>
         <DefaultValue>502</DefaultValue>
         <MaxValue></MaxValue>
         <MinValue></MinValue>
     </Communications>
</NewDataSet>

我当前的绑定xml到datagrid的方式如下:

<DataGrid Name="dgCommunication" ItemsSource="{Binding CommunicationsView}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ParamName" Binding="{Binding ParamName}" IsReadOnly="True"/>
        <DataGridTextColumn Header="ParamValue" Binding="{Binding ParamValues, Mode=TwoWay}" />
        <DataGridTextColumn Header="DefaultValue" Binding="{Binding DefaultValue}" IsReadOnly="True"/>
        <DataGridTextColumn Header="MaxValue" Binding="{Binding MaxValue}" IsReadOnly="True"/>
        <DataGridTextColumn Header="MinValue" Binding="{Binding MinValue}" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>

ViewModel中的ItemsSource如下:

private ICollectionView _ModulesView;
public ICollectionView ModulesView
{
    get
    {
        if (_ModulesView == null)
            RefreshModules();
        return _ModulesView;
    }
    set
    {
        _ModulesView = value;
        NotifyPropertyChanged();
    }
}

private void RefreshModules()
{
    ModulesView = new ListCollectionView(sdb.GetModules())
    {
        Filter = obj =>
        {
            var Module= (Module)obj;
            return SelectedProduct != null && Product.ModelNumber == SelectedProduct.ModelNumber;
        }
    };
}

public ObservableCollection<Module> GetModules()
{
    DataSet ds = StoreDbDataSet.ReadDataSet();
    ObservableCollection<Module> modules = new ObservableCollection<Module>();
    foreach (DataRow moduleRow in ds.Tables["Modules"].Rows)
    {
        modules.Add(new Module((UInt16)moduleRow["ModelNumber"], moduleRow["ParamName"].ToString(),
                    (UInt16)moduleRow["ParamValue"], (UInt16)moduleRow["DefaultValue"], (UInt16)moduleRow["MaxValue"],
                    (UInt16)moduleRow["MinValue"], (UInt16)moduleRow["Address"]));
    }
    return modules;
}

上面的代码工作正常。现在我想将一些datagrid单元格更改为comboBox。例如,我想让ParamValue可选,所以我可以像这样更改我的xml文件:

<NewDataSet>
    <Communications>
        <ModelNumber>0x01</ModelNumber>
        <ParamName>BaudRate</ParamName>
        <ParamValues>
            <ParamValue>9600</ParamValue>
            <ParamValue>19200</ParamValue>
            <ParamValue>115200</ParamValue>
        </ParamValues>
        <DefaultValue>502</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
</NewDataSet>

我尝试这样做:

<Window.DataContext>
<XmlDataProvider x:Name="Dataxml" Source="store.xml" />
</Window.DataContext>

<DataGridTemplateColumn Header="ParamValues">                                                   
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding XPath=ParamValue}"/>
        </DataTemplate>                                               
    </DataGridTemplateColumn.CellTemplate>                                                 

但它不起作用,我应该怎么做才能做到这一点?提前谢谢!

0 个答案:

没有答案