开发者

databinding propertygrid to property shows correctly in XAML preview engine but fails on runtime

开发者 https://www.devze.com 2023-03-10 12:10 出处:网络
First off, I am using the MVVM pattern with the Prism libraries. My application shell has two Regions: a main diagram region containing diagram shapes and items, and an auxiliary region as a kind of f

First off, I am using the MVVM pattern with the Prism libraries. My application shell has two Regions: a main diagram region containing diagram shapes and items, and an auxiliary region as a kind of frame on the left side. So far so good: the regionmanager loads the regions correctly into my shell.

My goal is to have a PropertyGrid in the auxiliary region show certain properties of a selected diagram item which is inside a canvas inside a listbox within the main diagram region. One of the properties of a diagram item is the string "Token". I am very close to making it work, and indeed the XAML preview engine shows all the properties in the p开发者_StackOverflow中文版ropertygrid when I hardcode my diagram item to be the SelectedItem my propertyGrid binds to, but once I run the program the propertyrgid remains empty. In other words, the XAML preview engine shows the desired result, but once I run the program, the result isn't the same.

Whan can be going wrong then?

My PropertyGrid's XAML:

        <local:PropertyGrid AutomaticlyExpandObjects="False"  
                         Width="200" 
                         Margin="5"
                         x:Name="propertyGrid" 
                         ShowDescription="False" 
                         ShowPreview="False"
                         Instance="{Binding Path=SelectedItem}">
        </local:PropertyGrid>

Property code within my PropertyGrid's DataContext:

    /// <summary>
    /// The state that is currently selected.
    /// </summary>
    public StateViewModel SelectedItem
    {
        get
        {
            return m_selectedItem;
        }
        set
        {
            if (m_selectedItem == value)
                return;

            m_selectedItem = value;

            RaisePropertyChanged("SelectedItem");
        }
    }

As I said, and what's most weird to me is the XAML preview engine correctly shows the SelectedItems properties like I would love them to be, but once I run the program, the propertygrid remains empty.

Can anyone help? Thanks in advance.


Most-likely it's a binding issue. Your DataContext might not be what you expect or the INotifyPropertyChanged events for your property might not be firing like you think they should.

Bea Stollnitz has a great article on the subject: http://bea.stollnitz.com/blog/?p=52

In particular, this nugget and observing the output window will tell you a lot:

<Window …
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
/>
<TextBlock Text="{Binding Path=Caption, diagnostics:PresentationTraceSources.TraceLevel=High}" … />
0

精彩评论

暂无评论...
验证码 换一张
取 消