locked
Using a Binding as a Resource

    Question

  • I have an element in my XAML in which I want to programmatically switch the value of an attribute between a specific value and a binding. I could obviously do this entirely programmatically, but I wanted to create a Binding resource by putting it in the Resources section. Here is the code I have in my Resources:

    <Binding x:Key="TotalsVisibility" Path="IsChecked" ElementName="radCumulative" Converter="{StaticResource BooleanVisibility}"/>

    When I use this in my XAML directly as follows:

    Visibility="{Binding IsChecked,ElementName=chkDisplayRound,Converter={StaticResource BooleanVisibility}}"

    It works fine. However, if I try to bind to the Resource using the following:

    Visibility="{StaticResource TotalsVisibility}"

    I receive the following error:

    An object of the type "Windows.UI.Xaml.Data.Binding" cannot be applied to a property that expects the type "Windows.UI.Xaml.Visibility".

    Is there some other way to store a Binding in the resources? Do I need to use some other way to access the resource? Any help would be appreciated. Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, October 21, 2014 12:47 AM

Answers

  • My reason for this is because I find it easier to design Bindings using XAML. What I ended up doing was defining the Bindings in my Resources, as in my original post, and then using them from the codebehind by accessing the Resources property. This worked, so it basically accomplished my purpose (being able to define the Binding in XAML), which means I guess I can call this a good alternative.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 22, 2014 1:18 PM

All replies

  • Hi Nathan,

    I've never see a binding to a Binding, why do you want to do so?

    If you would like programmatically binding, you could use code Binding class.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, October 22, 2014 7:37 AM
    Moderator
  • My reason for this is because I find it easier to design Bindings using XAML. What I ended up doing was defining the Bindings in my Resources, as in my original post, and then using them from the codebehind by accessing the Resources property. This worked, so it basically accomplished my purpose (being able to define the Binding in XAML), which means I guess I can call this a good alternative.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 22, 2014 1:18 PM