Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Binding combobox content of a property in propertygrid

$
0
0
Hi,

EditorDefinition is obsolete. Use EditorTemplateDefinition instead of EditorDefinition.

When using an EditorTemplateDefinition, you will target for a specific property : "Image". What will be under this EditorTemplateDefinition, is a PropertyItem with a "Value" of "the content of the Image property". To set the ComboBox to a list that is not the value of the Image property, you will have to use a staticResource list (defined in XAML) or go up the Ancestor until finding one with the CustomKeyImageList property. This is what the following sample does :
<xctk:PropertyGrid x:Name="_propertyGrid"
                         SelectedObject="{Binding}">
         <xctk:PropertyGrid.EditorDefinitions>
            <xctk:EditorTemplateDefinition TargetProperties="Image">
               <xctk:EditorTemplateDefinition.EditingTemplate>
                  <DataTemplate>
                     <Grid>
                        <Grid.ColumnDefinitions>
                           <ColumnDefinition Width="5*" />
                           <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <ComboBox Grid.Column="0"
                                  x:Name="ImageCombo"
                                  SelectionChanged="Image_OnSelectionChanged"
                                  SelectedItem="{Binding Value}"
                                  ItemsSource="{Binding Path=CustomKeyImageList, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Mode=OneWay}"
                                  Initialized="ImageCombo_OnInitialized">
                        </ComboBox>
                        <Button Grid.Column="1"
                                Content="..."
                                Click="OnAddCustomKeyImagesButtonClick"></Button>
                     </Grid>
                  </DataTemplate>
               </xctk:EditorTemplateDefinition.EditingTemplate>
            </xctk:EditorTemplateDefinition>
         </xctk:PropertyGrid.EditorDefinitions> 
      </xctk:PropertyGrid>

public partial class MainWindow : Window
  {
    public List<string> CustomKeyImageList
    {
      get;
      set;
    }

    public MainWindow()
    {
      Xceed.Wpf.Toolkit.Licenser.LicenseKey = "XXXXX-XXXXX-XXXXX-XXXX";

      InitializeComponent();

      CustomKeyImageList = new List<string>() { "string1", "string2", "string3" };

      this.DataContext = new UserObject() { Image = CustomKeyImageList[ 1 ] };
    }

    private void Image_OnSelectionChanged( object sender, SelectionChangedEventArgs e )
    {
    }

    private void OnAddCustomKeyImagesButtonClick( object sender, RoutedEventArgs e )
    {
    }

    private void ImageCombo_OnInitialized( object sender, EventArgs e )
    {
    }
  }

  public class UserObject : DependencyObject
  {
    public string Image
    {
      get;
      set;
    }
  }
You can have a look at the LiveExplorer Application available on this page : https://wpftoolkit.codeplex.com/ . Look for sample Data/PropertyGrid/Editors/EditorTemplateDefinition.

Viewing all articles
Browse latest Browse all 4964

Trending Articles