wpf DataGridTemplateColumn CellEditingTemplate - combobox的itemssource问题

时间:2014-03-05 12:19:13

标签: c# wpf datagrid tabcontrol datagridtemplatecolumn

我有两个项目的TabControl。

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <TabControl>
                <TabItem Header="Tab1" Content="{Binding}" ContentTemplate="{StaticResource Tab1}"/>
                <TabItem Header="Tab2" ContentTemplate="{StaticResource Tab2}"/>
            </TabControl>
        </Grid>
    </Window>

TabControl的一个tabItem中有DataGrid控件。 TabItems的DataTemplates:

    <DataTemplate x:Key="Tab1">
        <DataGrid ItemsSource="{Binding Entities}" x:Name="dataGridEx">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"/>
                <DataGridTemplateColumn Header="Position" Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Position}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox SelectedItem="{Binding Position, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                      ItemsSource="{Binding Path=DataContext.Positions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DataTemplate>

    <DataTemplate x:Key="Tab2">
        <Grid>
            <TextBox Text="Empty tab"></TextBox>
        </Grid>
    </DataTemplate>

MainWindow的DataContext:

public class MainWindowViewModel
{
    public ObservableCollection<Entity> Entities { get; set; }

    public List<string> Positions { get { return new List<string>() { "Manager", "Seller" }; } }

    public MainWindowViewModel()
    {
        Entities = new ObservableCollection<Entity>()
                       {
                           new Entity() {Name = "John", Position = "Manager"},
                           new Entity() {Name = "Mark", Position = "Seller"},
                           new Entity() {Name = "Alice"}
                       };
    }

}

实体类:

public class Entity : INotifyPropertyChanged
{
    private string _name;
    private string _position;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }
    public string Position
    {
        get { return _position; }
        set
        {
            _position = value;
            OnPropertyChanged("Position");
        }
    }

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    #endregion
}

应用程序正在运行。我编辑了Position列。然后我再次切换到第二个选项卡,然后再切换到第一个选项卡。已编辑行的位置值将被删除。

如果我直接将数据写入tabItems - 它可以正常工作

        <Grid>
            <TabControl>
                <TabItem Header="Tab1">...</TabItem> 
                <TabItem Header="Tab2">...</TabItem> 
            </TabControl>
        </Grid>

但我需要在我的解决方案中使用DataTemplates for tabItems。

我有点想法。在编辑模式中使用样式而非datatemplate

    <DataGridComboBoxColumn Header="Position" SelectedItemBinding="{Binding Position, UpdateSourceTrigger=PropertyChanged}">
            <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Position, ElementName=dataGridEx}" />
                    </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Variables, ElementName=dataGridEx}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>

通过这种方式一切正常。

但我有自定义控件,如IntelliSense而不是ComboBox。需要将DataGridTemplateColumn与DataTemplate一起用于CellTemplate和CellEditingTemplate。在这种情况下我该怎么办?也许我需要创建自定义DataGridComboBoxColumn?

你能帮助我解决我的问题吗?

0 个答案:

没有答案