Hi,
I've got a strange behavior regarding bindings. When I try to bind a property of a tool, for example the ButtonTool.Caption to my ViewModel, the binding never happens. (There are no binding errors listed in the Output window in VS). However, a binding does occur if I create my ViewModel as a resource and reference it using key throught StaticResource. Is this a bug? Why does the binding never occur?So this piece of code "works";
<
UserControl x:Class="IGDialog.MainPage"xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:x=http://schemas.microsoft.com/winfx/2006/xamlxmlns:d=http://schemas.microsoft.com/expression/blend/2008xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006xmlns:ig=infragistics.com/Controlsmc:Ignorable="d"xmlns:local="clr-namespace:IGDialog"d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources> <local:DataItem x:Key="DataItemHolder"/></UserControl.Resources><Grid x:Name="LayoutRoot" Background="White"> <ig:XamWebRibbon> <ig:XamWebRibbon.Tabs> <ig:XamWebRibbonTabItem Header="Tab1"> <ig:XamWebRibbonGroup Caption="Group 1"> <ig:ButtonTool Caption="{Binding Path=MyCaption, Source={StaticResource DataItemHolder}}"> </ig:ButtonTool> </ig:XamWebRibbonGroup> </ig:XamWebRibbonTabItem> </ig:XamWebRibbon.Tabs> </ig:XamWebRibbon></Grid></UserControl>
And this piece of code DOES NOT work;
<UserControl x:Class="IGDialog.MainPage"xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:x=http://schemas.microsoft.com/winfx/2006/xamlxmlns:d=http://schemas.microsoft.com/expression/blend/2008xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006xmlns:ig=http://infragistics.com/Controlsmc:Ignorable="d"xmlns:local="clr-namespace:IGDialog"d:DesignHeight="300" d:DesignWidth="400"><Grid x:Name="LayoutRoot" Background="White"> <ig:XamWebRibbon> <ig:XamWebRibbon.Tabs> <ig:XamWebRibbonTabItem Header="Tab1"> <ig:XamWebRibbonGroup Caption="Group 1"> <ig:ButtonTool Caption="{Binding Path=MyCaption}"> </ig:ButtonTool> </ig:XamWebRibbonGroup> </ig:XamWebRibbonTabItem> </ig:XamWebRibbon.Tabs> </ig:XamWebRibbon></Grid></UserControl>
public
MainPage(){ InitializeComponent(); this.DataContext = new DataItem();}
Thanks!
I was trying to bind a Command to a ribbon tool today, and i find a workaround for binding.
What i did was to create a Helper Dependency Object
DependencyObject
{
Context
)GetValue(ContextProperty); }
); }
}
// Using a DependencyProperty as the backing store for DataContext. This enables animation, styling, binding, etc...
ContextProperty =
));
and in the resources of the user control i added the following
<res:DataContextHelper x:Key="TEST" Context="{Binding ElementName=LayoutRoot, Path=DataContext}"/>
Then i added the following to the RibbonTool
<ig:ButtonTool Id="Btn1" Caption="{Binding Source={StaticResource TEST}, Path=Context.MyCaption}"
commands:Click.Command="{Binding Source={StaticResource TEST}, Path=Context.TestButtonToolClicked }" />
It won't work in design time but when you run it it will get the binding.
This is a workaround for prism commanding also
Regards,
Michael
Thanks, I noticed that ButtonTool does not have a DataContext, so I figured this should do the trick;Caption="{Binding Path=DataContext.MyCaption, ElementName=LayoutRoot}"
but that does not work either...? Any other suggestions?
So the reason the second example doesn't work is because the ButtonTool is not a FrameworkElement and therefore - it doesn't have DataContext. It's derived from DependencyObject and, since Silverlight 4, you can bind it's properties but you have to provide a valid Source.
Hope that helps,