WPF Datagrid下拉框

时间:2016-05-03 05:11:56

标签: c# wpf xaml datagrid mahapps.metro

我用combobox创建了一个datagrid表,我没有任何错误,但是当我尝试点击编辑表时,我得到了这个错误。 我正在使用mahapps.metro。在另一个空项目中,我创建这个代码完美无瑕。那么问题出在哪里,我应该如何解决呢?

A

问题在于:An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

这是XAML代码:

Error 1   The resource "StatusList" could not be resolved.

这里是ic c#code

<DataGrid x:Name="dgData" AutoGenerateColumns="False" Margin="169,10,10,227">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Subject}" Header="GAME SETTINGS" Width="340"/>
                            <DataGridTemplateColumn Header="" Width="360">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Status}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>

                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <ComboBox Height="20" IsEditable="True"
                                      ItemsSource="{StaticResource StatusList}"
                                      SelectedItem="{Binding Status}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>

                            </DataGridTemplateColumn>

                        </DataGrid.Columns>
                    </DataGrid>

1 个答案:

答案 0 :(得分:0)

如果你想绑定组合框项目源,那么只需在组合框的ItemsSource中使用Binding。

  <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox Height="20" IsEditable="True"
                                      ItemsSource="{Binding StatusList}"
                                      SelectedItem="{Binding Status}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>



   List<TicketInfo> ticketsList = new List<TicketInfo> 
            {
                new TicketInfo{ Subject="Show Ping", Status="True",StatusList=new List<string>(){"True","False"}},
                new TicketInfo{ Subject="Show Drawings", Status="True",StatusList=new List<string>(){"True","False"}},
                new TicketInfo{ Subject="Send Debug Messages", Status="True",StatusList=new List<string>(){"True","False"}},
                new TicketInfo{ Subject="Enable Default Profile", Status="False",StatusList=new List<string>(){"True","False"}}
            };



public class TicketInfo
    {
        public string Subject { get; set; }

        private string _status;
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
        private List<string> _statusList = new List<string>();
        public List<string> StatusList
        {
            get { return _statusList; }
            set { _statusList = value; }
        }
    }