WPF PropertyGrid 不应用属性值

发布于 2024-11-18 23:23:47 字数 1727 浏览 3 评论 0原文

我正在使用这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-11-25 23:23:47

您需要编辑源代码并更改文本编辑器上的绑定,以便它使用 UpdateSourceTrigger=PropertyChanged

要查找需要更新的源代码区域,您可以使用 Snoop 来检查控件。

运行您的应用程序,启动 snoop,从 Snoop 工具的下拉菜单中选择您的应用程序,然后单击双筒望远镜。现在,如果您在将光标悬停在控件上时按住 shiftctrl 键,您将能够看到其类型及其所有属性。

之后,您只需搜索解决方案以找到该类型并编辑 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 and ctrl 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.

放飞的风筝 2024-11-25 23:23:47

我不太清楚这个网格(我使用 这个 ),但我也有同样的问题。这似乎是一个常见问题。在选择新对象或清除所选对象属性之前,尝试将焦点从 PropertyGrid 移至另一个控件。例如:

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

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:

    public static void UpdatePropertyGridObjects(object objToSelect)
    {
        Components.DockManager.Focus();
        Components.PropertyGrid.SelectedObject = objToSelect;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文