我开发了一个usercontrol,里面有一个画布。我在画布上画了一些线条和弧线。每当datacontext发生更改时,我都需要在更新新的datacontext之前强制usercontrol的datacontext为null。除非新图纸没有更新。这是为什么?如何更新Usercontrol的内容(实际上是canvas的内容),而不是在更新新的datacontext之前将datacontext重置为null。
我使用以下代码。 FlashControl是包含canvas内部的usercontrol。
(FlashControl.Content as FrameworkElement).DataContext = null;
(FlashControl.Content as FrameworkElement).DataContext = this.DataContext;
FlashControl.DataContext = this.DataContext;
更新 :::::::这是我的用户控件
<UserControl x:Class="PricingRoutine.FlashGeneratorUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Converters="clr-namespace:PricingRoutine.Converters"
xmlns:local ="clr-namespace:CustomFlashGenerator"
mc:Ignorable="d" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<UserControl.Resources>
<Converters:TextPositionConverter x:Key="TextPositionConverter" />
<Converters:LinetPositionConverter x:Key="LinePositionConverter" />
<Converters:PointPositionConverter x:Key="PointPositionConverter" />
</UserControl.Resources>
<Grid x:Name="MajorGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl ItemsSource="{Binding Path = CompositeObjectList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!--<ViewBox Stretch="Uniform">-->
<Canvas x:Name="MainCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="False">
<Canvas.RenderTransformOrigin>
<Point X="0.5" Y="0.5" />
</Canvas.RenderTransformOrigin>
<Canvas.RenderTransform>
<TransformGroup>
<TranslateTransform X="0" Y="0" /> <!--Handle this one through binding-->
<ScaleTransform ScaleX="1" ScaleY="1" CenterX=".5" CenterY=".5" /> <!--Handle this one through binding-->
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Line}">
<Line Stroke="Black" StrokeThickness="1">
<Line.X1>
<MultiBinding Converter="{StaticResource LinePositionConverter}" ConverterParameter="X1">
<Binding ElementName="MainCanvas" Path="ActualWidth" />
<Binding ElementName="MainCanvas" Path="ActualHeight" />
<Binding Path = "StartPoint.X" />
</MultiBinding>
</Line.X1>
<Line.Y1>
<MultiBinding Converter="{StaticResource LinePositionConverter}" ConverterParameter="Y1">
<Binding ElementName="MainCanvas" Path="ActualWidth" />
<Binding ElementName="MainCanvas" Path="ActualHeight" />
<Binding Path = "StartPoint.Y" />
</MultiBinding>
</Line.Y1>
<Line.X2>
<MultiBinding Converter="{StaticResource LinePositionConverter}" ConverterParameter="X2">
<Binding ElementName="MainCanvas" Path="ActualWidth" />
<Binding ElementName="MainCanvas" Path="ActualHeight" />
<Binding Path = "EndPoint.X" />
</MultiBinding>
</Line.X2>
<Line.Y2>
<MultiBinding Converter="{StaticResource LinePositionConverter}" ConverterParameter="Y2">
<Binding ElementName="MainCanvas" Path="ActualWidth" />
<Binding ElementName="MainCanvas" Path="ActualHeight" />
<Binding Path = "EndPoint.Y" />
</MultiBinding>
</Line.Y2>
</Line>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Grid>
</UserControl>