问题描述
我从此链接获得了对propertygrid控件的初始帮助,以启动和显示示例数据。我的要求是基于一个事件,我应该从其视图模型中更改属性网格的SelectedObject值。
我的XAML包含具有扩展wpf工具包的PropertyGrid的Usercontrol
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>
和我的ViewModelPropertyGridViewModel是
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;
}
}
当我运行此代码时,它没有在PropertyGrid中显示任何属性。如果我在.CS文件中有相同的代码,我可以看到CodeBookName属性,因为这个属性在CodeBookViewModel中可用。
我在这里做错了吗?
根据事件,我将使用不同的属性将SelectedObject更改为另一个ViewModel。可能吗?我是否必须在XAML中拥有所有viewmodel的属性?或者我可以通过创建属性从ViewModels动态生成吗?
请帮助我。提前致谢。希望我已经正确解释了我的问题。
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.
推荐答案
这篇关于将WPF扩展工具包PropertyGrid的SelectedObject值动态设置为不同的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!