Skip to main content

Microsoft Silverlight

Answered Question TwoWay binding to ResourcesRSS Feed

(0)

slyi
slyi

Participant

Participant

824 points

254 Posts

TwoWay binding to Resources

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

slyi

<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
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: TwoWay binding to Resources

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.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

slyi
slyi

Participant

Participant

824 points

254 Posts

Re: TwoWay binding to Resources

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>
 

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Re: TwoWay binding to Resources

It won't two way bind without the path. The datacontext isn't the issue.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Re: Re: TwoWay binding to Resources

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.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

slyi
slyi

Participant

Participant

824 points

254 Posts

Answered Question

Re: Re: Re: TwoWay binding to Resources

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities