2011-12-01 5 views
0

DataGrid가 포함 된 WPF 사용자 정의 컨트롤이 있습니다. 이 DG에는 상태에 대한 ComboBox를 비롯한 여러 열이 포함되어 있습니다. 상태 목록이 채워지고 내 ViewModel에 속성으로 저장됩니다.내 WPF DataGrid의 ComboBox에 항목이 표시되지 않습니다.

StateList 속성을 내 Combobox의 ItemsSource에 바인딩하려고하지만 양식을 실행하고 DG를 편집하려고 할 때 콤보 상자에 값이 없으면 콤보 상자가 비어 있습니다.

다음은 usercontrol의 XAML입니다.

<UserControl x:Class="myproject.View.ucContactView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" d:DesignHeight="475" d:DesignWidth="977"> 
<UserControl.Resources> 
    <ResourceDictionary Source="/Templates/MyResourceDictionary.xaml"/> 
</UserControl.Resources> 
<Grid DataContext="{Binding ViewModel}"> 
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding AddressCollectionViewSource.View}"> 
     <DataGridTemplateColumn Header="State" Width="160"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding StateDescription}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
      <DataGridTemplateColumn.CellEditingTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <ComboBox Name="cboState" 
            SelectedValuePath="StateKey" 
            ItemTemplate="{StaticResource dtStateTemplate}" 
            ItemsSource="{Binding StateList}" 
            SelectedItem="{Binding StateKey, Mode=TwoWay}" 
            Width="100" /> 
        </StackPanel> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellEditingTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid> 
</Grid> 
</UserControl> 

이상한 것은 내가 똑같은 콤보와 함께이 UserControl을 다른 콤보를 만들 경우 예상대로,이 콤보 상자가 작동하는 것입니다.

<!-- this works as long as it's not in the DG --> 
<StackPanel Height="126" HorizontalAlignment="Left" Margin="766,275,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="200" > 
    <ComboBox Name="cboState2" 
      SelectedValuePath="StateKey" 
      ItemTemplate="{StaticResource dtStateTemplate}" 
      ItemsSource="{Binding StateList}" 
      SelectedItem="{Binding StateKey, Mode=TwoWay}" 
      Width="100" /> 
</StackPanel> 

DG의 콤보 박스가 StateList 속성의 값을 표시하지 않는 이유는 무엇입니까? 왜 분리 콤보 박스가 제대로 작동합니까?

+0

가 왜'DataGridComboBoxColumn'을 사용할 수 없습니다? –

답변

2

그것은 작동하지 않습니다 할 수 t의 재산으로 그는 DataGridDataContext입니다. 즉, ViewModel.StateList에 바인딩해야 할 때 ViewModel.AddressCollectionViewSource.View.StateList에 바인딩하려고합니다. 디버깅 중에 출력 창을 확인하면 Could not find property StateList on object AddressCollectionViewSource (or maybe ICollection)의 효과에 바인딩 오류가 표시됩니다.

대신이 시도 :

<ComboBox Name="cboState2" 
     SelectedValuePath="StateKey" 
     ItemTemplate="{StaticResource dtStateTemplate}" 
     ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
      AncestorType={x:Type DataGrid}}, Path=DataContext.StateList}" 
     SelectedItem="{Binding StateKey, Mode=TwoWay}" 
     Width="100" /> 
1

당신의 ViewModel 창에서 속성 인 경우에는 해당 ComboBoxStateList을 찾고 있기 때문에이

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ViewModel.StateList, Mode=OneWay}" 


<Window x:Class="WpfStackOverflowSpielWiese.Window2" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window2" 
     Height="300" 
     Width="300" 
     x:Name="window"> 

    <Grid DataContext="{Binding ElementName=window, Path=ViewModel}"> 

    <DataGrid x:Name="grid" 
       AutoGenerateColumns="False" 
       ItemsSource="{Binding AddressCollectionViewSource, Mode=OneWay}"> 

     <DataGrid.Columns> 
     <DataGridTemplateColumn Header="State" 
           Width="160"> 

      <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding StateKey}" /> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 

      <DataGridTemplateColumn.CellEditingTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
       <ComboBox Name="cboState" 
          SelectedValuePath="StateKey" 
          ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ViewModel.StateList, Mode=OneWay}" 
          SelectedItem="{Binding StateKey, Mode=TwoWay}" 
          Width="100" /> 
       </StackPanel> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellEditingTemplate> 

     </DataGridTemplateColumn> 
     </DataGrid.Columns> 

    </DataGrid> 
    </Grid> 
</Window> 


using System.Collections.ObjectModel; 
using System.Windows; 

namespace WpfStackOverflowSpielWiese 
{ 
    /// <summary> 
    /// Interaction logic for Window2.xaml 
    /// </summary> 
    public partial class Window2 : Window 
    { 
    public static readonly DependencyProperty ViewModelProperty = 
     DependencyProperty.Register("ViewModel", typeof(ViewModelClass), typeof(Window2), new PropertyMetadata(default(ViewModelClass))); 

    public ViewModelClass ViewModel { 
     get { return (ViewModelClass)this.GetValue(ViewModelProperty); } 
     set { this.SetValue(ViewModelProperty, value); } 
    } 

    public Window2() { 
     this.InitializeComponent(); 
     this.grid.Items.Clear(); 
     this.ViewModel = new ViewModelClass(); 
    } 
    } 

    public class StateClass : DependencyObject 
    { 
    public static readonly DependencyProperty StateKeyProperty = 
     DependencyProperty.Register("StateKey", typeof(string), typeof(ViewModelClass), new PropertyMetadata(default(string))); 

    public string StateKey { 
     get { return (string)this.GetValue(StateKeyProperty); } 
     set { this.SetValue(StateKeyProperty, value); } 
    } 

    public static readonly DependencyProperty StateProperty = 
     DependencyProperty.Register("State", typeof(string), typeof(StateClass), new PropertyMetadata(default(string))); 

    public string State { 
     get { return (string)this.GetValue(StateProperty); } 
     set { this.SetValue(StateProperty, value); } 
    } 
    } 

    public class ViewModelClass : DependencyObject 
    { 
    public static readonly DependencyProperty StateListProperty = 
     DependencyProperty.Register("StateList", typeof(ObservableCollection<string>), typeof(ViewModelClass), new PropertyMetadata(default(ObservableCollection<string>))); 

    public static readonly DependencyProperty AddressCollectionViewSourceProperty = 
     DependencyProperty.Register("AddressCollectionViewSource", typeof(ObservableCollection<StateClass>), typeof(ViewModelClass), new PropertyMetadata(default(ObservableCollection<StateClass>))); 

    public ObservableCollection<StateClass> AddressCollectionViewSource { 
     get { return (ObservableCollection<StateClass>)this.GetValue(AddressCollectionViewSourceProperty); } 
     set { this.SetValue(AddressCollectionViewSourceProperty, value); } 
    } 

    public ObservableCollection<string> StateList { 
     get { return (ObservableCollection<string>)this.GetValue(StateListProperty); } 
     set { this.SetValue(StateListProperty, value); } 
    } 

    public ViewModelClass() { 
     this.StateList = new ObservableCollection<string>(new[] {"one", "two"}); 
     this.AddressCollectionViewSource = new ObservableCollection<StateClass>(new[] {new StateClass {State = "state", StateKey = "one"}}); 
    } 
    } 
} 
관련 문제