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

New Post: Custom ItemSource for combobox in wpftoolkit propertygrid

$
0
0
Hi,

Maybe something like this sample may help you :
<Window x:Class="WpfApplication52.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
      <StackPanel>
         <xctk:PropertyGrid x:Name="_propertyGrid">
            <xctk:PropertyGrid.EditorDefinitions>
               <xctk:EditorTemplateDefinition TargetProperties="MyFirstComboBoxString">
                  <xctk:EditorTemplateDefinition.EditingTemplate>
                     <DataTemplate>
                        <ComboBox ItemsSource="{Binding FirstComboBoxData, RelativeSource={RelativeSource AncestorType=Window}}"
                                  SelectionChanged="FirstComboBox_SelectionChanged" />
                     </DataTemplate>
                  </xctk:EditorTemplateDefinition.EditingTemplate>
               </xctk:EditorTemplateDefinition>
               <xctk:EditorTemplateDefinition TargetProperties="MySecondComboBoxString">
                  <xctk:EditorTemplateDefinition.EditingTemplate>
                     <DataTemplate>
                        <ComboBox ItemsSource="{Binding SecondComboBoxData, RelativeSource={RelativeSource AncestorType=Window}}"/>
                     </DataTemplate>
                  </xctk:EditorTemplateDefinition.EditingTemplate>
               </xctk:EditorTemplateDefinition>
            </xctk:PropertyGrid.EditorDefinitions>               
         </xctk:PropertyGrid>

         <Button Content="Add FirstComboBox Source"
                 Click="Button_Click" />
      </StackPanel>
   </Grid>
</Window>

public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _propertyGrid.SelectedObject = new Person()
      {
        FirstName = "Mike",
        LastName = "Smith"
      };
    }

    #region FirstComboBoxData

    public static readonly DependencyProperty FirstComboBoxDataProperty = DependencyProperty.Register( "FirstComboBoxData", typeof( IList ), typeof( MainWindow ), new UIPropertyMetadata( null ) );
    public IList FirstComboBoxData
    {
      get
      {
        return ( IList )GetValue( FirstComboBoxDataProperty );
      }
      set
      {
        SetValue( FirstComboBoxDataProperty, value );
      }
    }

    #endregion //FirstComboBoxData

    #region SecondComboBoxData

    public static readonly DependencyProperty SecondComboBoxDataProperty = DependencyProperty.Register( "SecondComboBoxData", typeof( IList ), typeof( MainWindow ), new UIPropertyMetadata( null ) );
    public IList SecondComboBoxData
    {
      get
      {
        return ( IList )GetValue( SecondComboBoxDataProperty );
      }
      set
      {
        SetValue( SecondComboBoxDataProperty, value );
      }
    }

    #endregion //SecondComboBoxData

    private void Button_Click( object sender, RoutedEventArgs e )
    {
      this.FirstComboBoxData = new List<string>() { "First", "Second", "Third" };
    }

    private void FirstComboBox_SelectionChanged( object sender, SelectionChangedEventArgs e )
    {
      this.SecondComboBoxData = new List<string>() { "one", "two", "three" };
    }
  }

  public class Person
  {
    public string FirstName
    {
      get;
      set;
    }
    public string LastName
    {
      get;
      set;
    }
    public string MyFirstComboBoxString
    {
      get;
      set;
    }
    public string MySecondComboBoxString
    {
      get;
      set;
    }
  }

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>