0

We have made a WPF usercontrol consisting of a Label with a nested Textblock as Content (for textwrapping). Because we drag and drop usercontrols like this one, we use no names in the XAML-Definition because there is a problem with creating a copy of the usercontrol. So we use instead a viewmodel. DependencyProperties of the textblock - like FontSize - are bound to the property of the ancestor (the UserControl).

Per example:

        <TextBlock Text="{Binding Path=Text, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                   FontSize="{Binding RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=FontSize}"

And in the CS-file of the UserControl we override the FontSize-property of the UserControl, so that we can get and set it:

    public new double FontSize
    {
        get { return base.FontSize; }
        set { base.FontSize = value; }
    }

Now the problem is, that we want to have the possibility to edit the usercontrols properties after dropped on a WPF window.

For this we are opening after the Drop-event a Dialog and give the UserControl as FrameworkElement to it. Then in that Dialog we can change p.e. the FontSize. The setter of the FontSize property above is called and base.FontSize is changed. When we take a look to the property of the given element in the Debugger, the FontSize is changed. But the visual FontSize of the TextBlock in our usercontrol is still the same.

How can we make a refresh?

Thanks in advance!

P.S.: When we drop an UserControl on the second WPF-window we make a copy of the control by XAML-serialization and -deserialization. After the first step - the serialization - we get a string like that for the Label-UserControl:

<ControlLabel Text="Label1" ControlFontSize="12" ControlFontWeight="Normal" ControlFontStyle="Normal" ControlHorizontalContentAlignment="Left" ControlVerticalContentAlignment="Top" BorderBrush="#FF00FFFF" BorderThickness="1,1,1,1" av:Canvas.Left="103" av:Canvas.Top="214" xmlns="clr-namespace:WpfDesignerControlLibrary;assembly=WpfDesignerControlLibrary" xmlns:assembly="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <ControlLabel.TextDecorations>
    <assembly:Null />
  </ControlLabel.TextDecorations>
  <av:Grid>
    <av:Grid.ColumnDefinitions>
      <av:ColumnDefinition />
    </av:Grid.ColumnDefinitions>
    <av:Grid.RowDefinitions>
      <av:RowDefinition Height="*" />
    </av:Grid.RowDefinitions>
    <av:Border BorderThickness="0,0,0,0" BorderBrush="{assembly:Null}">
      <av:Border.Style>
        <av:Style TargetType="av:IFrameworkInputElement">
          <av:Style.Resources>
            <av:ResourceDictionary />
          </av:Style.Resources>
          <av:Setter Property="av:Border.BorderBrush">
            <av:Setter.Value>
              <av:SolidColorBrush>#FF4169E1</av:SolidColorBrush>
            </av:Setter.Value>
          </av:Setter>
          <av:Setter Property="av:Border.BorderThickness">
            <av:Setter.Value>
              <av:Thickness>1,1,1,1</av:Thickness>
            </av:Setter.Value>
          </av:Setter>
          <av:Setter Property="av:FrameworkElement.HorizontalAlignment">
            <av:Setter.Value>
              <assembly:Static Member="av:HorizontalAlignment.Stretch" />
            </av:Setter.Value>
          </av:Setter>
          <av:Setter Property="av:FrameworkElement.VerticalAlignment">
            <av:Setter.Value>
              <assembly:Static Member="av:VerticalAlignment.Stretch" />
            </av:Setter.Value>
          </av:Setter>
        </av:Style>
      </av:Border.Style>
      <av:Label HorizontalContentAlignment="Left" VerticalContentAlignment="Top" Margin="2,2,2,2">
        <av:TextBlock Text="Label1" FontStyle="Normal" FontWeight="Normal" FontSize="12" TextAlignment="Left" TextWrapping="Wrap">
          <av:TextBlock.TextDecorations>
            <assembly:Null />
          </av:TextBlock.TextDecorations>
        </av:TextBlock>
      </av:Label>
    </av:Border>
  </av:Grid>
</ControlLabel>

Can it be that the serialization did some damage to the control? As i said above, the setter works and the property changes, but the text in the control stays the same.

1 Answer 1

0

The problem is that the seriaization removes the bindings. This can be avoided by creating a new instance of the dragged control instead of copying it by XAML-serialization und -deserialization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.