Persistence Framework kills binding

4 posts, 0 answers
  1. Stefan
    Stefan avatar
    19 posts
    Member since:
    Jul 2012

    Posted 21 Nov 2012 Link to this post

    Hello,

    I bind 2 Controls

    <telerik:RadToggleButton x:Name="ShowLogButton"  Grid.Column="1" Grid.Row="1" Content="..."></telerik:RadToggleButton>
    <telerik:RadListBox ItemsSource="{Binding Path=Log}" DisplayMemberPath="Description" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Height="70" Visibility="{Binding ElementName=ShowLogButton, Path=IsChecked,  Converter={StaticResource ControlVisibilityConverter}}">
    </telerik:RadListBox>

    Follow Visible attribute, after saving and loading my window with Persistence Framework my toggle button is broken and dont works anymore

    This code helps

    <telerik:RadToggleButton x:Name="ShowLogButton"  Grid.Column="1" Grid.Row="1" Content="..."></telerik:RadToggleButton>
    <telerik:RadListBox ItemsSource="{Binding Path=Log}" DisplayMemberPath="Description" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Height="70" Visibility="{Binding ElementName=ShowLogButton, Path=IsChecked,  Converter={StaticResource ControlVisibilityConverter}}">
    <telerik:PersistenceManager.SerializationOptions>
        <telerik:SerializationMetadataCollection>
            <telerik:PropertyNameMetadata Condition="Except" Expression="Visibility" SearchType="PropertyName" />
        </telerik:SerializationMetadataCollection>
    </telerik:PersistenceManager.SerializationOptions>
    </
    telerik:RadListBox>

    Should i use this for every binded Porperty? Can i exclude any binded properties from serialisation ?
  2. Stefan
    Stefan avatar
    19 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2012 Link to this post

    its the same if i have a simple textbox, binding is broke after reloading

    <TextBlock Text="{Binding Name}" MaxWidth="280" TextWrapping="Wrap" Margin="5,0,0,0"  Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" />
  3. UI for WPF is Visual Studio 2017 Ready
  4. Stefan
    Stefan avatar
    19 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2012 Link to this post

    can i except whole control from serialisation if i serialize my window ?
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 26 Nov 2012 Link to this post

    Hi Stefan,

    When using the PersistenceFramework, you need to keep in mind that the framework saves the values of the UIProperties of all UIElements within the persisted control. This is why when you have a binding, the persistence manager will take the value defined by the binding and well save it as the value of the property in question. So if you have a binding for the Visibility property and the binding sets the Visibility to False, the persistence manager will save the Visibility property with value of False. This is why the approach for removing the bound properties out of the list with saved properties is a good approach.

    And in case, you'd like to remove certain controls from the list pf persisted properties, you'll need to track the properties that define them. For example if you have a Window with one TextBlock in it, like so:
    <Window x:Class="WpfApplication1.MainWindow"
            Title="MainWindow">
        <TextBlock Text="{Binding Name}"
                   MaxWidth="280"
                   TextWrapping="Wrap"
                   Margin="5,0,0,0"
                   Grid.Row="0"
                   Grid.Column="1"
                   VerticalAlignment="Top" />
    </Window>
    Then you can remove the TextBlock from the list of persisted controls, by removing the Window.Content property from the list of persisted Window properties.
    <Window x:Class="WpfApplication1.MainWindow"
            Title="MainWindow">
            <telerik:PersistenceManager.SerializationOptions>
                <telerik:SerializationMetadataCollection>
                    <telerik:PropertyNameMetadata Condition="Except"
                                         Expression="Content"
                                         SearchType="PropertyName" />
                </telerik:SerializationMetadataCollection>
            </telerik:PersistenceManager.SerializationOptions>
            <TextBlock Text="{Binding Name}"
                       MaxWidth="280"
                       TextWrapping="Wrap"
                       Margin="5,0,0,0"
                       Grid.Row="0"
                       Grid.Column="1"
                       VerticalAlignment="Top" />
    </Window>

    Instead,if the Window structure is such that the TextBlock is part of a Grid panel:
    <Window x:Class="WpfApplication1.MainWindow"
            Title="MainWindow">
        <Grid>
            <TextBlock Text="{Binding Name}"
                       MaxWidth="280"
                       TextWrapping="Wrap"
                       Margin="5,0,0,0"
                       Grid.Row="0"
                       Grid.Column="1"
                       VerticalAlignment="Top" />
        </Grid>
    </Window>
    Then you need to remove the Grid.Children property from the list of persisted properties:
    <Window x:Class="WpfApplication1.MainWindow"
            Title="MainWindow">
        <Grid>
            <telerik:PersistenceManager.SerializationOptions>
                <telerik:SerializationMetadataCollection>
                    <telerik:PropertyNameMetadata Condition="Except"
                                     Expression="Children"
                                     SearchType="PropertyName" />
                </telerik:SerializationMetadataCollection>
            </telerik:PersistenceManager.SerializationOptions>
            <TextBlock Text="{Binding Name}"
                       MaxWidth="280"
                       TextWrapping="Wrap"
                       Margin="5,0,0,0"
                       Grid.Row="0"
                       Grid.Column="1"
                       VerticalAlignment="Top" />
        </Grid>
    </Window>

    Basically you can define a SerializationOptions collection for each control in your Window. You only need to have in mind that you basically persist the properties of the main control (for example the Window), but if its Content/Items/Header/Children define other controls, in order to not persist them, you have to exclude the Content/Items/Header/Children property from the parent control. 

    Let me know if that information helps.

    Kind regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top