【问题标题】:Use complex fields for XamDataGrid为 XamDataGrid 使用复杂字段
【发布时间】:2016-06-08 14:10:34
【问题描述】:

我有一个带有 ObservableCollection 项目的 XamDataGrid 作为数据源。 一个 Item 有两个属性 A 和 B 并且 A 和 B 都有一个 Value 属性。我想将这两个 Value 属性绑定到我的两个 Fields。

显然这样的事情是行不通的:

<Field Name="A.Value"/>

我读过一些关于 AlternateBinding 的文章,但这对我也不起作用:

<Field AlternateBinding="A.Value"/>

我没有找到关于如何使用 AlternateBinding 的好教程,所以我真的不知道如何使用它。

编辑:

当我使用这样的 UnboundField 时它可以工作:

<UnboundField Name="Value" BindingPath="A.Value" BindingMode="TwoWay"/>

但是它说 UnboundField 已被弃用,我应该使用 Field。我希望我知道怎么做。

【问题讨论】:

    标签: c# wpf xaml observablecollection xamdatagrid


    【解决方案1】:

    将您的项目上的属性设置为公开,并将字段名称设置为您的属性名称。

    例子:

        <Window x:Class="WPFXamDataGrid.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-mpatibility/2006"
                xmlns:igDP="http://infragistics.com/DataPresenter"
                xmlns:local="clr-namespace:WPFXamDataGrid"
                mc:Ignorable="d"
                Title="MainWindow" Height="350" Width="525">
            <Grid>
                <igDP:XamDataGrid x:Name="xamDataGrid" Loaded="XamDataGrid_Loaded">
                    <igDP:XamDataGrid.FieldLayouts>
                        <igDP:FieldLayout>
                            <igDP:Field Name="Name"/>
                            <igDP:Field Name="Date"/>
                        </igDP:FieldLayout>
                    </igDP:XamDataGrid.FieldLayouts>
                </igDP:XamDataGrid>
            </Grid>
        </Window>
    
    
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace WPFXamDataGrid
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void XamDataGrid_Loaded(object sender, RoutedEventArgs e)
            {
                ObservableCollection<Item> list = new ObservableCollection<Item>();
    
                list.Add(new Item {Name = "First", Date = DateTime.Now});
                list.Add(new Item { Name = "Second", Date = ateTime.Now.AddDays(-1);
    
                xamDataGrid.DataSource = list;
            }
        }
    
        class Item
        {
            public string Name { get; set; }
            public DateTime Date { get; set; }
        }
    }
    

    编辑一个:

    <Style x:Key="CellStyle" TargetType="{x:Type igDP:CellValuePresenter}">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}}">
                            <TextBlock Text="{Binding Path=Value.Length}" FontWeight="Bold" />
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
    </Style>
    
    <igDP:Field Name="Name" CellValuePresenterStyle="{StaticResource CellStyle}"/>
    

    【讨论】:

    • 请再看看我的问题。在你的例子中,我不想绑定'Name',而是'Name.Length',例如。
    • 您可以将 Name.Length 公开为 Item 的公共属性。然后将同名的 XamDataGrid 字段绑定(简单地声明)到该属性。
    • 我正在寻找一种直接绑定到子属性的方法,而无需创建新类型。有点像UnboundField
    • 您可以像在 EDIT ONE 中那样创建新样式并将其应用到字段中。
    【解决方案2】:

    其实你已经差不多了。

    <Field Name="Test" AlternateBinding="{Binding A.Value}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多