2016-10-03 3 views
0

Wenn ich Auswahl in Kombinationsfeld Debugger ändern TestSelectedItem {set} in User Klasse treffen wird, aber ich mag Debugger TestSelectedItem {set} in MainWindow Klasse schlagen. Bitte suggeriert mir die richtige Lösung.WPF Datenrasterdaten Combobox Spalt ausgewähltes Element Bindung in C#

public class User 
{ 
    public string Name { get; set; } 
    public ObservableCollection<string> TypeCollection { get; set; }  

    private string _testSelectedItem; 
    public string TestSelectedItem 
    { 
     get { return _testSelectedItem; } 
     set { _testSelectedItem = value; } 
    } 

    public User() 
    { 
     TypeCollection = new ObservableCollection<string>(); 
     TypeCollection.Add("A"); 
     TypeCollection.Add("B"); 
     TypeCollection.Add("C"); 
    } 
} 

public partial class MainWindow : Window 
{ 
    public List<User> Users { get; set; } 

    private string _testSelectedItem; 
    public string TestSelectedItem 
    { 
     get { return _testSelectedItem; } 
     set { _testSelectedItem = value; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Users = new List<User>(); 
     Users.Add(new User() { Name = "User 1" }); 
     Users.Add(new User() { Name = "User 2" }); 
     Users.Add(new User() { Name = "User 3" }); 
     myGrid.DataContext = this; 
    } 
} 

<Window x:Class="WpfApplication11.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-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApplication11" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid Name ="myGrid"> 
    <StackPanel> 

     <DataGrid Name="MyDataGrid" AutoGenerateColumns="False"    ItemsSource="{Binding Users}"> 

     <DataGrid.Columns>    

      <DataGridTextColumn Binding="{Binding Name}" 
           Header="Name" 
           Width="90" 
           CanUserResize="False" 
           CanUserSort="False" 
           CanUserReorder="False" 
           IsReadOnly="True" /> 

       <DataGridComboBoxColumn Header="Product Type" 
             DisplayMemberPath="ProductType" 
             SelectedValuePath="ProductTypeId" 
             SelectedValueBinding="{Binding SelectedValuePath, UpdateSourceTrigger=PropertyChanged}"> 

        <DataGridComboBoxColumn.ElementStyle> 
         <Style TargetType="{x:Type ComboBox}"> 
          <Setter Property="ItemsSource" Value="{Binding TypeCollection}"/> 
          <Setter Property="VerticalAlignment" Value="Center"/> 
         </Style> 
        </DataGridComboBoxColumn.ElementStyle> 

        <DataGridComboBoxColumn.EditingElementStyle> 
         <Style TargetType="{x:Type ComboBox}"> 
          <Setter Property="ItemsSource" Value="{Binding TypeCollection}"/> 
          <Setter Property="VerticalAlignment" Value="Center"/> 
         </Style> 
        </DataGridComboBoxColumn.EditingElementStyle> 
       </DataGridComboBoxColumn> 

       <DataGridTemplateColumn Header="Bug"> 
        <DataGridTemplateColumn.CellTemplate > 
         <DataTemplate> 
          <ComboBox Height="25" Width="200" 
             x:Name ="ppCombo" 
             ItemsSource="{Binding TypeCollection }" 
             SelectedItem="{Binding TestSelectedItem ,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}"> 
          </ComboBox> 

         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 

     </DataGrid>   

     </StackPanel> 

    </Grid> 
</Window> 

Antwort

0

Ich glaube, ich habe gerade eine Lösung, aber nicht sicher ist, dass die richtige

SelectedItem = "{Binding Relative = {Relative FindAncestor, AncestorType = {x: Typ local: Mainwindow}}, Pfad = TestSelectedItem} ">

Verwandte Themen