【问题标题】:Template Bindings with UnboundFields in XamDataGridXamDataGrid 中具有 UnboundFields 的模板绑定
【发布时间】:2013-07-18 21:03:04
【问题描述】:

全部。我有一个 XamDataGrid 对象;每个对象都有两个我想在网格中编辑的属性。 我正在以编程方式创建我的 FieldLayout,以允许不同数量的列。 当我定义我的布局时,我可以为每个字段设置绑定,以便 PropertyPath 指向我的 ViewModel 中的特定列((即 Path="Columns[0]"、Path="Columns[1]" 等)工作正常:

http://imgur.com/rJLCKiR

我想将一个模板应用到 CellValuePresenter 以让我编辑每个字段,并将一个文本框绑定到两个字段中的每一个:

http://imgur.com/9IEq1Du

如您所见,模板绑定不正确。现在,模板已绑定到其中一列 (Path="DataItem.Columns[0]"),因为我无法找到一种简单的方法来将每列的索引放入模板中。我真正想要的是让 CellValuePresenter 获得正确的绑定对象,在我的 FieldLayout 中定义,但将每个文本框绑定到适当的属性。

任何帮助将不胜感激。 谢谢, 埃德

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:igWPF="http://schemas.infragistics.com/xaml/wpf"
          xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">


    <Window.Resources>
        <ResourceDictionary>
            <local:InfoConverter x:Key="InfoConverter" />

            <Style TargetType="{x:Type igWPF:CellValuePresenter}" x:Key="InfoStyle">
                <Setter Property="BorderThickness" Value="2"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type igWPF:CellValuePresenter}">
                            <StackPanel>
                                <Border BorderBrush="Black" BorderThickness="2">
                                    <StackPanel>
                                        <StackPanel Orientation="Vertical">
                                            <Label Content="Name:" />
                                            <TextBox Text="{Binding Path=DataItem.Columns[0], ConverterParameter=name, Converter={StaticResource InfoConverter}}"/>
                                        </StackPanel>
                                        <StackPanel Orientation="Vertical">
                                            <Label Content="Age:" />
                                            <TextBox Text="{Binding Path=DataItem.Columns[0], ConverterParameter=age, Converter={StaticResource InfoConverter}}"/>
                                        </StackPanel>
                                    </StackPanel>                                        
                                </Border>                                    
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <igWPF:XamDataGrid x:Name="Grid"  DataSource="{Binding Rows, Mode=TwoWay}">
            <igWPF:XamDataGrid.FieldLayouts>
                <igWPF:FieldLayout>
                    <igWPF:FieldLayout.Fields>

                    </igWPF:FieldLayout.Fields>
                </igWPF:FieldLayout>
            </igWPF:XamDataGrid.FieldLayouts>
            <igWPF:XamDataGrid.FieldLayoutSettings>
                <igWPF:FieldLayoutSettings SelectionTypeCell="Single" SelectionTypeRecord="Single" AutoGenerateFields="False"/>
            </igWPF:XamDataGrid.FieldLayoutSettings>
        </igWPF:XamDataGrid>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WpfApplication2
{
    class InfoConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string mode = parameter as string;
            Info info = value as Info;
            if (mode != null && info != null)
            {
                if (mode.Equals("name"))
                {
                    return info.Name;
                }
                else if (mode.Equals("age"))
                {
                    return info.Age;
                }
            }            
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}

using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication2
{
    public class RowViewModel :NotificationObject
    {
        public RowViewModel(params Info[] info) : base()
        {
            foreach (Info i in info){
                Columns.Add(i);   
            }
        }

        private ObservableCollection<Info> columns = new ObservableCollection<Info>();
        public ObservableCollection<Info> Columns
        {
            get
            {
                return columns;
            }
            set
            {
                columns = value;
                RaisePropertyChanged("Columns");
            }
        }
    }

      public class TableViewModel :NotificationObject
      {
        public TableViewModel() : base()
        {

            Rows.Add(new RowViewModel(new Info("A", 1), new Info( "B", 2), new Info("C", 3)));
            Rows.Add(new RowViewModel(new Info("D", 4), new Info( "E", 5), new Info("F", 6)));
            Rows.Add(new RowViewModel(new Info("G", 7), new Info("H", 8), new Info("I", 9)));
        }

        private ObservableCollection<RowViewModel> rows = new ObservableCollection<RowViewModel>();
        public ObservableCollection<RowViewModel> Rows
        {
            get
            {
                return rows;
            }
            set
            {
                rows = value;
                RaisePropertyChanged("Rows");
            }
        }
    }

      public class Info
      {
          public Info(string name, int age)
          {
              this.Name = name;
              this.Age = age;
          }

          public override string ToString()
          {
              return Name + " " + Age;
          }

          private string _name;
          public string Name
          {
              get
              {
                  return _name;
              }

              set
              {
                  _name = value;
              }
          }

          private int _age;
          public int Age
          {
              get
              {
                  return _age;
              }
              set
              {
                  this._age = value;
              }
          }
      }
}

【问题讨论】:

    标签: wpf templates binding xamdatagrid


    【解决方案1】:

    编辑:我想通了!我只是绑定到 CellValuePresenter 的值。

    <TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igWPF:CellValuePresenter}}, Path=Value, ConverterParameter=name, Converter={StaticResource InfoConverter}}"/>
    

    见鬼,我什至不需要转换器:

    <TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igWPF:CellValuePresenter}}, Path=Value.Age}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 2016-02-16
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多