Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
changing the DataContext of a TilePane at runtime
posted

I have this brief code snippet below that tries to bind the DataContext of a TilePane at runtime. When the TilePane is maximized, however, the text does not show. Can anyone point out the problem in the code?

 

<UserControl x:Class="ResultsViewer.MainPage" 

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

             xmlns:igChart="clr-namespace:Infragistics.Silverlight.Chart;assembly=Infragistics.Silverlight.Chart.v9.2">

    <UserControl.Resources>

        <DataTemplate x:Key="Comment">

            <StackPanel>

                <TextBlock Text="Comment"/>

            </StackPanel>

        </DataTemplate>

        <DataTemplate x:Key="CommentHeader">

            <StackPanel>

                <TextBlock Text="Comment Header"/>

            </StackPanel>

        </DataTemplate>

        <DataTemplate x:Key="CommentMaximized">

            <StackPanel>

                <TextBlock Text="{Binding Comment}"/>

            </StackPanel>

    </UserControl.Resources>

        <igTV:XamWebTileView RowsInPage="3" ColumnsInPage="3" x:Name="xamWebTileView" MaximizedStateChanging="XamWebTileView_MaximizedStateChanging">

        </igTV:XamWebTileView>

</UserControl>

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(TileViewBindingViewModel_Loaded);

        }

 

        void TileViewBindingViewModel_Loaded(object sender, RoutedEventArgs e)

        {

            TilePane comment = new TilePane {

                ContentTemplate = this.Resources["Comment"] as DataTemplate,

                HeaderTemplate = this.Resources["CommentHeader"] as DataTemplate,

                DataContext = new { Comment = "Test" },

            };

            this.xamWebTileView.Items.Add(comment);

 

        }

        void XamWebTileView_MaximizedStateChanging(object sender, TileStateChangingEventArgs e)

        {

            TilePane pane = e.Element as TilePane;

            if (pane == null)

                return;

                switch(e.NewState)

                {

                    case TileState.Normal:

                        pane.ContentTemplate = this.Resources["Comment"] as DataTemplate;

                        break;

                    case TileState.Minimized:

                        pane.ContentTemplate = this.Resources["Comment"] as DataTemplate;

                        break;

                    case TileState.Maximized:

                        pane.ContentTemplate = this.Resources["CommentMaximized"] as DataTemplate;

                        break;

                    default:

                        break;

                }

        }

 

    }

Parents
No Data
Reply
  • 3071
    posted

    Hi,
    There are two problems in the code.
    1. Anonymous type
    You attempt to data bind to an anonymous type but anonymous Types are defined internal to the assembly that they are used in.
    2. ContentControl and ContentTemplate
    TilePane is a ContentControl and the ContentTemplate property is related to the Content property, not to the DataContext.

    Everything works fine if we use some public type (Point for example) and set the Content like in this sample:
    <DataTemplate x:Key="Comment">
        <StackPanel>
            <TextBlock Text="{Binding X}"/>
        </StackPanel>
    </
    DataTemplate>
    <DataTemplate x:Key="CommentMaximized">
        <StackPanel>
           
    <TextBlock Text="{Binding Y}"/>
        </StackPanel>
    </
    DataTemplate>

    void TileViewBindingViewModel_Loaded(object sender, RoutedEventArgs e)
    {
        TilePane comment = new TilePane
        {
            ContentTemplate =
    this.Resources["Comment"] as DataTemplate,
            HeaderTemplate =
    this.Resources["CommentHeader"] as DataTemplate,
            DataContext =
    new Point(1000, 2000),
        };
        comment.SetBinding(
    TilePane.ContentProperty, new Binding());
        this.xamWebTileView.Items.Add(comment);
    }
    Regards,
    Marin

Children
No Data