Hi,
I apologise in advance for what's probably a simple WPF data binding question.
I'm trying to bind the AllowEdit field setting on the XamDataGrid to some property on my control's data context, but I can't find the right syntax. What I've got is something like the following:
<UserControl>
<XamDataGrid>
<DataPresenter:XamDataGrid.FieldSettings>
<DataPresenter:FieldSettings AllowEdit="{Binding MyAllowEdit}" />
</DataPresenter:XamDataGrid.FieldSettings>
...
</XamDataGrid>
</UserControl>
Where MyAllowEdit is a property on the object that is the DataContext of my User Control.
I've tried a number of different bindings, but I just can't figure out how to do it.
Cheers,
Adam
Hello Adam,
You are most likely not getting change notification because the FieldSettings object is not part of the Visual Tree. In circumstances like this, I typically suggest that people handle the PropertyChanged event of their DataContext on their Window or UserControl and set the property manually:
void myDataContext_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "MyAllowEdit") { //check new value and apply settings to the grid } }
Hope that helps.
Thanks for the answer. At the moment, we don't actually need to listen for changes, so I'm just setting the value when the screen loads. The main reason that I was trying to use the binding was because I was trying to do as much as possible in XAML and avoid the code behind if possible.
Is there any other way to set this value from XAML (to the value in the DataContext)?
Adam,
I am not quite sure if this is natively supported in the xaml only.
You can create a style for the underlying editor for the IsReadOnly property. This however, will allow the cell to go into edit mode, but it will be readonly. If that is acceptable in your scenario, you can try something like this :
<Style TargetType="{x:Type igEditors:XamTextEditor}">
<Setter Property="IsReadOnly" Value="{Binding Path=Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
</Style>
Another way in the procedural code would be to handle the EditModeStarting event and cancel it if needed.
if(!MyProperty)
e.Cancel = true;
In theory you should be able to bind dependency properties on the Field and field's Settings. The reason why your original code though is not working is that because the Field and its FieldSettings are not part of the visual tree, so those objects do not have access to the DataContext. There are cases where this will work for non-visual objects within the WPF framework (e.g. GridViewColumn which is used in a listview to define a column) but only because it is using some internal members to associate an inheritance context with that object. Unfortunately MS has not made the ability to propogate an inheritance context available publicly so it is not possible to do this in xaml for these objects. There are a few options though. Josh posted about one option here. He has a more generalized approach provided on codeproject to provide an "artificial" inheritance context here. Lastly, you can create the binding in code. Note, since SetBinding is on the Framework(Content)Element classes, you would need to use the BindingOperations class to apply the binding in code.
Thanks for the replies. I think for now we'll continue to set the value when the screen loads, but we may move to using a style like you suggest at some point once we have a better idea about how we're organizing our styles and how we can do this in a re-usable fashion.