简体   繁体   English

C#GridView-具有可空数据的组合框列

[英]C# GridView - Combobox Column with nullable data

I'm trying to create a RadGridView that is populated by a call to a WCF that return a query that includes some null values. 我正在尝试创建一个RadGridView,它由对WCF的调用填充,该调用返回包含一些空值的查询。 I need for one of these values, an integer describing 'Roles', to be the only writeable field, accessed through a combobox column. 我需要这些值之一(一个描述'Roles'的整数)作为唯一可通过组合框列访问的可写字段。 I need to have the combobox display the same 4 choices in the dropdown, regardless if the value is not, but labeled with a string that is not apart of the query results. 我需要让组合框在下拉列表中显示相同的4个选择,无论值是否不是,而是用属于查询结果的字符串标记。

All the other columns are read-only and could be automatically generated, but I want to apply some custom formatting based on the value of the 'Roles' field. 所有其他列都是只读的,并且可以自动生成,但是我想根据“角色”字段的值应用一些自定义格式。

I'm making the WCF call and setting RadGridView.ItemSource to the result in the code-behind. 我正在进行WCF调用,并将RadGridView.ItemSource设置为隐藏代码中的结果。

private void ExpandGet(object sender, Telerik.Windows.RadRoutedEventArgs e)
{ GetContent(4, CurrentUser.Id); }

private void GetContent(int libType, int userId)
{
    Services.Libraries.Do(d1 => Dispatcher.ExecuteSync(
          () =>
          {
              var res = d1.EndGetRole(d1.BeginGetRole(userId, libType, null, null));
              return res;
          },
      init: () => WaitContent1.IsBusy = true,
      result: res =>
      {
          Content1.ItemsSource = res;
      },
      finalize: ok1 => WaitContent1.IsBusy = false));
}

I tried creating a separate list for the Roles column to use for its DisplayMember Path, and add that to the Autogenerated DataGrid, but it doesn't add anything and returns a blank grid. 我尝试为“角色”列创建一个单独的列表以用于其DisplayMember路径,然后将其添加到“自动生成的数据网格”中,但是它不添加任何内容并返回空白网格。

public class RIdName
    {
        public int RId { get; set; }
        public string RName { get; set; }
    }                       

    public List<IdName> RItemsSource;

    public void SetGridUp()
    {

        RItemsSource.Add(new IdName {RId = -1, RName = "None"});
        RItemsSource.Add(new IdName {RId = 0, RName = "User"});
        RItemsSource.Add(new IdName {RId = 1, RName = "Contributer"});
        RItemsSource.Add(new IdName {RId = 2, RName = "Manager"});

        GridViewComboBoxColumn column = new GridViewComboBoxColumn();
        column.DataMemberBinding = new Binding("Role");
        column.DisplayMemberPath = "Rname";
        column.Header = "My Column";
        column.UniqueName = "MyColumn";
        column.ItemsSource = RItemsSource;
        Content1.AutoGenerateColumns = false;
        Content1.Columns.Add(column);
    }

I also tried to do it through XAML andchange the DisplayMemberPath from there, but since the strings that I need to display are not in the original query and ItemSource I have nothing to set it to. 我也尝试通过XAML进行操作并从那里更改DisplayMemberPath,但是由于我需要显示的字符串不在原始查询和ItemSource中,因此我没有任何设置。 In the table I get the integer values but no choices at all in the dropdown. 在表中,我得到了整数值,但下拉列表中根本没有选择。

<telerik:RadGridView x:Name="Content1"
     ItemsSource="{Binding}" 
     AutoGenerateColumns="False" 
     CanUserDeleteRows="False" 
     CanUserInsertRows="False">
 <telerik:RadGridView.Columns>
   <telerik:GridViewDataColumn DataMemberBinding="{Binding LibraryName}"     UniqueName="Name" />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding Per}" UniqueName="Per" />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding By}" UniqueName="By" />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding On}" UniqueName="On" IsReadOnly="True" />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding Role} DisplayMemberBinding="IdName" UniqueName="Role" />
 </telerik:RadGridView.Columns>  
</telerik:RadGridView>

Finally, I need to use this Grid with the same query but for different content type in about 15 different grids on the page. 最后,我需要在同一查询中使用此Grid,但要在页面上约15个不同的网格中针对不同的内容类型使用此Grid。 I would ideally like to create a template of some kind and reuse it. 理想情况下,我想创建某种模板并重用它。

What approach should I take to deal with this combobox column? 我应该采取什么方法来处理此组合框列? I tried using a converter (which calls a WCF service) but I just got back the integers and no dropdown selections. 我尝试使用转换器(该调用WCF服务),但我只获取了整数,没有下拉选项。 I need the column to show the actually values, and I want to be able to define the dropdown choices somewhere in the code-behind. 我需要该列来显示实际值,并且我希望能够在后面的代码中的某处定义下拉选项。

You're close with trying to do it through XAML, it's something I do often. 您即将尝试通过XAML进行操作,这是我经常做的事情。 What you need to do is bind it to a property in the ViewModel using the ItemsSource property of the GridViewComboBoxItem. 您需要执行的操作是使用GridViewComboBoxItem的ItemsSource属性将其绑定到ViewModel中的属性。 To do that, you'll need to set a reference to the ViewModel as a StaticResource. 为此,您需要将对ViewModel的引用设置为StaticResource。

<UserControl.Resources>
    <local:MainPageViewModel x:Key="mainPageViewModel" />
</UserControl.Resources>
...
<telerik:GridViewComboBoxColumn Header="Role"
        DataMemberBinding="{Binding RID}"
        ItemsSource="{Binding Path=RItemSource, Source={StaticResource mainPageViewModel}}"
        DisplayMemberPath="RName"
        SelectedValueMemberPath="RID" />

You can find some more info here if needed. 如果需要,您可以在这里找到更多信息。 http://docs.telerik.com/devtools/silverlight/controls/radgridview/troubleshooting/blank-cells http://docs.telerik.com/devtools/silverlight/controls/radgridview/troubleshooting/blank-cells

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM