none
how to set a property of a static resource via a binding

    Question

  • This must be a really stupid question because I asked on stackoverflow and got no answer.  With apologies for my inability to grasp this I will ask again here:

    In the XAML fragment below, ItemsSource is set correctly for "testGrid" but for "testGridResource" it is null. How do I set the ItemsSource of the grid defined as a resource via a binding? More generally, how do I set a property of an object defined as a static resource via a binding (I am only using datagrid as an example in my app I am using other objects)?

    <Window x:Class="StackedBarTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ig="http://schemas.infragistics.com/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="root">
    <Window.Resources>
        <DataGrid x:Key="testGridResource" ItemsSource="{Binding LoadStatistics, ElementName=root}"></DataGrid>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <DataGrid x:Name="testGrid" ItemsSource="{Binding LoadStatistics, ElementName=root}"></DataGrid>

    Monday, November 26, 2012 7:56 PM

Answers

  • Hi SWheaties,

    As for your original post:

    <Window.Resources>
        <DataGrid x:Key="testGridResource" ItemsSource="{Binding LoadStatistics, ElementName=root}"></DataGrid>
    </Window.Resources>

    Firstly, the binding could work in this scenario, however, resource will be parsed when it is used, for example:

    <Window.Resources>
        <TextBlock x:Key="textblock" Text="{Binding Title, ElementName=root}"/>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <ContentControl Content="{StaticResource textblock}"/>
    </Grid>

    you could use it as above way.

    As for your last reply, the binding will not work because your GroupBy control does not get the DataContext, you have to set the DataContext property in "ItemsSource="{Binding LoadStatistics}"".

    Additional, becaue the your control is Third-party control, so I suggest you post it on third party control forum to get better support.

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, November 28, 2012 8:09 AM
    Moderator

All replies

  • Can you show us all the XAML.  The part you showed shows testGridResource as a resource but nowhere is it shown where it is used.  Also some indication of where DataContext is set would be good.

    LS


    Lloyd Sheen

    Monday, November 26, 2012 10:52 PM
  • My example is contrived to illustrate my question - I would be happy with an an answer that showed me how to set the ItemSource and view it a watch window in the click event of a button.

    In any case, here is the code of my app where I am doing conceptualy the same thing. The object named AccountGroup that is defined as a resource needs to get its ItemsSource from the viewmodel.  The XamDataChart named barChart uses the AccountGroup object as an ItemsSource.  In my example the DataContext is set in the constructor of the Window: DataContext = this;

    XAML Fragment from my (its pretty long so I'm snipping stuff out)

    <Window.Resources>
            <ig:GroupBy x:Key="AccountGroup" KeyMemberPath="Source" GroupMemberPath="PeriodEnding" ValueMemberPath="AccountCount" ItemsSource="{Binding LoadStatistics}"></ig:GroupBy>
            
        </Window.Resources>
        <Grid>
            <ig:XamDock x:Name="xmDockContainer" Margin="10" Grid.Row="1">
                <Border CornerRadius="1" Margin="5" BorderThickness="1" Grid.Row="1">
                    <ig:XamDataChart x:Name="barChart" 
                                     Legend="{Binding ElementName=xmLegend}" 
                                     HorizontalZoomable="True"
                                     VerticalZoomable="True"
                                     >
            
                            <ig:StackedColumnSeries x:Name="stack"  
                                                    XAxis="{Binding ElementName=xmXAxis}"  
                                                    YAxis="{Binding ElementName=xmYAxis}"
                                                    ItemsSource="{StaticResource ResourceKey=AccountGroup}"
                                                    AutoGenerateSeries="True"
                                                    LegendItemTemplate ="{StaticResource ResourceKey=keyLegendItemTemplate}"
                                                    >
                            </ig:StackedColumnSeries>
                        </ig:XamDataChart.Series>
                    </ig:XamDataChart>

    Tuesday, November 27, 2012 12:08 AM
  • Hi SWheaties,

    As for your original post:

    <Window.Resources>
        <DataGrid x:Key="testGridResource" ItemsSource="{Binding LoadStatistics, ElementName=root}"></DataGrid>
    </Window.Resources>

    Firstly, the binding could work in this scenario, however, resource will be parsed when it is used, for example:

    <Window.Resources>
        <TextBlock x:Key="textblock" Text="{Binding Title, ElementName=root}"/>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <ContentControl Content="{StaticResource textblock}"/>
    </Grid>

    you could use it as above way.

    As for your last reply, the binding will not work because your GroupBy control does not get the DataContext, you have to set the DataContext property in "ItemsSource="{Binding LoadStatistics}"".

    Additional, becaue the your control is Third-party control, so I suggest you post it on third party control forum to get better support.

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, November 28, 2012 8:09 AM
    Moderator
  • Thank you Sheldon Xiao.

    The critical part of your answer is this:

    >>> resource will be parsed when it is used

    If you look at a property that is the target of a binding for a resource that is not actually used in xaml, the bound property will appear null.


    • Edited by SWheaties Wednesday, December 05, 2012 1:01 AM
    Wednesday, December 05, 2012 12:53 AM