简体   繁体   English

WPF PropertyGrid 不应用属性值

[英]WPF PropertyGrid doesn't apply property value

I'm using this property control for WPF from Denis Vuyka.我正在使用 Denis Vuyka 的 WPF 的属性控件。

I have the problem that it doesn't apply the new value of a property, if I don't press the TAB key.如果我不按 TAB 键,我的问题是它不会应用属性的新值。

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:到目前为止,这是我在 OnOK 中尝试的但无济于事:

        propertyGrid.Focus();
        propertyGrid.MoveFocus(new System.Windows.Input.TraversalRequest(System.Windows.Input.FocusNavigationDirection.Next));
        System.Windows.Forms.SendKeys.SendWait("{TAB}");

You'd need to edit the source code and change the binding on the text editor so that it uses UpdateSourceTrigger=PropertyChanged .您需要编辑源代码并更改文本编辑器上的绑定,以便它使用UpdateSourceTrigger=PropertyChanged

To find the area of source that needs updating you can use Snoop to inspect the control.要查找需要更新的源区域,您可以使用Snoop来检查控件。

Get your application running, fire up snoop, pick your application from the drop down menu on the Snoop tool, and click the binoculars.运行您的应用程序,启动 snoop,从 Snoop 工具的下拉菜单中选择您的应用程序,然后单击双筒望远镜。 Now if you hold shift and ctrl keys while you hover the cursor over the control you'll be able to see its type and all of its properties.现在,如果您在控制 hover 和 cursor 时按住shiftctrl键,您将能够看到它的类型及其所有属性。

After that you just need to search the solution to find that type and edit the binding in the XAML.之后,您只需搜索解决方案以找到该类型并编辑 XAML 中的绑定。 Take a look at this page for info on how to use the UpdateSourceTrigger binding property.查看页面以获取有关如何使用UpdateSourceTrigger绑定属性的信息。

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.在选择新的 object 或清除选定的 object 属性之前,尝试将焦点从 PropertyGrid 移到另一个控件。 For example:例如:

    public static void UpdatePropertyGridObjects(object objToSelect)
    {
        Components.DockManager.Focus();
        Components.PropertyGrid.SelectedObject = objToSelect;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM