views:

206

answers:

3
+1  Q: 

WPF Binding Help

I haven't used WPF that much so the solution to this is probably pretty easy.

In the ide I'm developing it will have multiple controls(text editor) each being hosted in a tab, much like VS does for each source file. When the user clicks new the "host" creates a new EditorWindow(a usercontrol), creates a new tab, and tells the tab to display the EditorWindow it created, and then updates a property called currentWindow (of type EditorWindow) with the one that's currently active. Inside the EditorWindow is the text editor whose name is textEditor(also a property). What I'm trying to do is take this code from the quick start source of the text editor control I'm using

                    <StackPanel>
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsIndicatorMarginVisible}" Content="Indicator margin visible" />
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsLineNumberMarginVisible}" Content="Line number margin visible" />
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsRulerMarginVisible}" Content="Ruler margin visible (useful for fixed-width fonts only)" />
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsSelectionMarginVisible}" Content="Selection margin visible" />
                    </StackPanel>

put that in the host controls xaml, and bind the checkboxes to the syntax editor. I've tried a couple different things to no avail. Control is the name of the window hosting all the tabs, and path is obviously supposed to be the property that the checkboxes are bound too. I'm pretty sure the problem is that at initial run-time currentWindow isn't initialized so therefore my bindings aren't ever getting updated, but I'm at a loss as to how to fix this issue. Thanks!

A: 

I've found that the Snoop utility is the easiest way to do quick binding debugging, you should try using it and see if it tells you anything useful on the bound properties.

kek444
A: 

Well now I'm more confused than I was before :-P

I downloaded snoop, ran my application, then snoop and found my app inside of it. When I snoop my app and click the drop down and select "Visuals with binding errors" the four checkboxes do in fact show up. When I select one in Snoop it then draws a red box around the checkbox in my application and it is at this moment that if I click on the checkbox everything works perfectly just like it's supposed to! The problem is, they still don't work unless Snoop has a red box around them. There's no binding errors on any property, the only thing I notice is that IsChecked's row has a green background and valuesource is set to local. I'll keep working with snoop, but anymore help would be appreciated.

Snoop offers a ContextMenu, something like "Show binding errors", you could post the error text for those "red" properties.
kek444
+1  A: 

Since you are new to WPF, you may not know that properties have to implement some sort of change notifications in order for bindings to work. For instance, if any of the properties in the the path "currentWindow.textEditor.IsIndicatorMarginVisible" change, you need to inform the binding engine that it has changed. If you implement these properties as DependencyPropertys, the change tracking comes for free. Otherwise, you should implement INotifyPropertyChanged.

Abe Heidebrecht