I got initial help for propertygrid control from this link to launch and display sample data. My requirement is based on an event, i should change the property grid's SelectedObject value from its viewmodel.
My XAML contains Usercontrol which has Extended wpf toolkit's PropertyGrid
<usercontrol x:class="Helper.View.PropertyGridUserControl" xmlns:x="#unknown">
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"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:vm="clr-namespace:Helper.ViewModel"
DataContext="{DynamicResource PropertyGridViewModel}"
mc:Ignorable="d"
xmlns:local="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit" >
<Usercontrol.resources>
<vm:propertygridviewmodel x:key="PropertyGridViewModel" xmlns:vm="#unknown" />
</Usercontrol.resources>
<Grid>
<xctk:PropertyGrid x:Name="_propertyGrid" Margin="10" SelectedObject="{Binding PropertyGridData}" AutoGenerateProperties="False">
<!-- Only the following properties will be displayed in the PropertyGrid -->
<xctk:PropertyGrid.PropertyDefinitions>
<xctk:PropertyDefinition Name="CodeBookName"/>
<xctk:PropertyDefinition Name="FavoriteColor"/>
<xctk:PropertyDefinition Name="PetNames"/>
</xctk:PropertyGrid.PropertyDefinitions>
</xctk:PropertyGrid>
</Grid>
and my ViewModel "PropertyGridViewModel" is
public class PropertyGridViewModel : ViewModelBase
{
PropertyGrid _propertyGrid;
public PropertyGrid PropertyGridData
{
get { return _propertyGrid; }
set
{
_propertyGrid = value;
OnPropertyChanged("PropertyGridData");
}
}
public PropertyGridViewModel()
{
CodeBookViewModel vm = new CodeBookViewModel();
PropertyGridData = new PropertyGrid();
PropertyGridData.PropertyDefinitions = new PropertyDefinitionCollection();
PropertyDefinition item = new PropertyDefinition();
string[] properties = { "CodeBookName", "CodeBookID" };
item.TargetProperties = properties;
PropertyGridData.PropertyDefinitions.Add(item);
PropertyGridData.SelectedObject = vm;
}
}
When I run this code, it is not showing any properties in PropertyGrid. If I have the same code in .CS file, I am able to see CodeBookName property, as this property is available in CodeBookViewModel.
Am I doing anything wrong here?
Based on the event, I will change the SelectedObject to another ViewModel here with different properties. Is it possible? Do I have to have all viewmodel's properties in XAML? or Can I generate on the fly from ViewModels by creating properties?
Please help me. Thanks in advance. Hope I have explained my question properly.