WPF PropertyGrid 不应用属性值
我正在使用这个 Denis Vuyka 的 WPF 属性控制。
我遇到的问题是,如果我不按 TAB 键,它不会应用属性的新值。
因此,如果我更改属性网格中的属性,然后单击“确定”按钮,该属性仍具有以前的值。
要重现的示例代码:
public partial class MainWindow : Window
{
DataObject dataObject = new DataObject();
public MainWindow()
{
InitializeComponent();
propertyGrid.SelectedObject = dataObject;
}
private void OnOK(object sender, RoutedEventArgs e)
{
MessageBox.Show("Value of test is " + dataObject.test);
}
}
class DataObject
{
public int test { get; set; }
public int test2 { get; set; }
}
<Window x:Class="PropGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:pg="http://schemas.denisvuyka.wordpress.com/wpfpropertygrid"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" IsDefault="True" Click="OnOK">OK</Button>
<pg:PropertyGrid x:Name="propertyGrid" Grid.Row="1">
</pg:PropertyGrid>
</Grid>
</Window>
只需在属性测试中键入一个数字,然后单击“确定”按钮。
有人知道这个问题的解决方法吗?
这是我迄今为止在 OnOK 中尝试过的方法,但没有成功:
propertyGrid.Focus();
propertyGrid.MoveFocus(new System.Windows.Input.TraversalRequest(System.Windows.Input.FocusNavigationDirection.Next));
System.Windows.Forms.SendKeys.SendWait("{TAB}");
I'm using this
property control for WPF from Denis Vuyka.
I have the problem that it doesn't apply the new value of a property, if I don't press the TAB key.
So if I change a property in the property grid and then click the OK button, the property has still the previous value.
Sample code to reproduce:
public partial class MainWindow : Window
{
DataObject dataObject = new DataObject();
public MainWindow()
{
InitializeComponent();
propertyGrid.SelectedObject = dataObject;
}
private void OnOK(object sender, RoutedEventArgs e)
{
MessageBox.Show("Value of test is " + dataObject.test);
}
}
class DataObject
{
public int test { get; set; }
public int test2 { get; set; }
}
<Window x:Class="PropGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:pg="http://schemas.denisvuyka.wordpress.com/wpfpropertygrid"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" IsDefault="True" Click="OnOK">OK</Button>
<pg:PropertyGrid x:Name="propertyGrid" Grid.Row="1">
</pg:PropertyGrid>
</Grid>
</Window>
Just type a number into property test and then click the OK button.
Does anybody know a workaround for this problem?
This is what I tried in OnOK so far to no avail:
propertyGrid.Focus();
propertyGrid.MoveFocus(new System.Windows.Input.TraversalRequest(System.Windows.Input.FocusNavigationDirection.Next));
System.Windows.Forms.SendKeys.SendWait("{TAB}");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要编辑源代码并更改文本编辑器上的绑定,以便它使用
UpdateSourceTrigger=PropertyChanged
。要查找需要更新的源代码区域,您可以使用 Snoop 来检查控件。
运行您的应用程序,启动 snoop,从 Snoop 工具的下拉菜单中选择您的应用程序,然后单击双筒望远镜。现在,如果您在将光标悬停在控件上时按住
shift
和ctrl
键,您将能够看到其类型及其所有属性。之后,您只需搜索解决方案以找到该类型并编辑 XAML 中的绑定。请查看此页面有关如何使用
UpdateSourceTrigger
绑定属性的信息。You'd need to edit the source code and change the binding on the text editor so that it uses
UpdateSourceTrigger=PropertyChanged
.To find the area of source that needs updating you can use Snoop to inspect the control.
Get your application running, fire up snoop, pick your application from the drop down menu on the Snoop tool, and click the binoculars. Now if you hold
shift
andctrl
keys while you hover the cursor over the control you'll be able to see its type and all of its properties.After that you just need to search the solution to find that type and edit the binding in the XAML. Take a look at this page for info on how to use the
UpdateSourceTrigger
binding property.我不太清楚这个网格(我使用 这个 ),但我也有同样的问题。这似乎是一个常见问题。在选择新对象或清除所选对象属性之前,尝试将焦点从 PropertyGrid 移至另一个控件。例如:
I do not know exactly for this grid (I use this one), but I have the same problem there. It seems to be a common problem. Try to remove focus from PropertyGrid to another control before selecting new object ot clearing selected object property. For example: