所有。我有一个XamDataGrid对象;每个对象都有两个我想在网格中编辑的属性。 我正在以编程方式创建FieldLayout,以允许不同数量的列。 当我定义我的布局时,我可以为每个Field设置绑定,以便PropertyPath指向我的ViewModel中的特定列((即Path =“Columns [0]”,Path =“Columns [1]”等)工作正常:
我想将一个模板应用到CellValuePresenter,让我编辑每个字段,一个文本框绑定到两个字段中的每一个:
如您所见,模板绑定不正确。现在,模板已绑定到其中一列(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;
}
}
}
}
答案 0 :(得分: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}"/>