Skip to main content
Home Forums Silverlight Programming Programming with .NET - General TwoWay binding to Resources
5 replies. Latest Post by slyi on November 7, 2009.
(0)
slyi
Participant
824 points
254 Posts
11-06-2009 3:32 PM |
Hi All,
Can anyone explain why resources in ItemsControl allow twoway binding while regular resources are oneway only?
eg: Paste the following into xamlpad http://silverlight.services.live.com/invoke/6655/XamlPadSL3/iframe.html
Thanks
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Grid.Resources> <ItemsControl x:Key="ResVars"> <sys:Double >32</sys:Double> </ItemsControl> <sys:Double x:Key="Dbl" >32</sys:Double> </Grid.Resources> <Grid x:Name="LayoutRoot"> <StackPanel> <Slider Value="{Binding Items[0], Source={StaticResource ResVars},Mode=TwoWay}" Maximum="100" /> <TextBlock Text="{Binding Items[0], Source={StaticResource ResVars}}"/> <!--<Slider Value="{Binding Source={StaticResource Dbl}, Mode=TwoWay}" Maximum="100" /> <TextBlock Text="{Binding Source={StaticResource Dbl}}"/>--> </StackPanel> </Grid> </Grid>
bryant
Star
9937 points
1,629 Posts
11-06-2009 3:47 PM |
If you load that in visual studio you will get the error message that two way bindings require a path. Here is the best explanation I've seen on that:
The problem is as you have described, you need a path, a binding without a path binds to the current datacontext. But in this situation, even if you could have a two-way binding, updating the source could only replace the datacontext - it couldn't actually set the value somewhere else because it doesn't know where it came from.
11-06-2009 4:15 PM |
ok but it still doesnt twoway bind when i set the datacontext
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" DataContext="{Binding RelativeSource={RelativeSource Self}}" > <Grid.Resources> <sys:Int32 x:Key="Dbl" >32</sys:Int32> </Grid.Resources> <Grid x:Name="LayoutRoot" > <StackPanel> <Slider Value="{Binding Path=Resources.Values[0], Mode=TwoWay}" Maximum="100" /> <TextBlock Text="{Binding Path=Resources.Values[0]}"/> </StackPanel> </Grid> </Grid>
11-06-2009 5:15 PM |
It won't two way bind without the path. The datacontext isn't the issue.
11-06-2009 5:16 PM |
What is it that you're really trying to do? If you really need to bind to a resouce just put the double in a class and you'd be done.
11-07-2009 10:21 AM |
Im trying to create xaml based binding relay for MVVM. I think I understand why now that ResourceDictionary does not inherits ObservableCollection while ItemsControl does.