C# WPF转换器性能不清楚
我正在尝试制作一个导航器,它显示C# WPF转换器性能不清楚,c#,wpf,converter,C#,Wpf,Converter,我正在尝试制作一个导航器,它显示ScrollViewer内容的缩略图。我的应用程序有一个工作区,我在其中放置对象,并希望在navigator中显示这些对象: <local:WorkArea x:Name="WorkArea" Grid.Row="1"/> 我的导航器也是一个用户控件: <local:Navigator DataContext="{Binding ElementName=WorkArea, Path=Content}" Hori
ScrollViewer
内容的缩略图。我的应用程序有一个工作区,我在其中放置对象,并希望在navigator中显示这些对象:
<local:WorkArea x:Name="WorkArea" Grid.Row="1"/>
我的导航器也是一个用户控件
:
<local:Navigator DataContext="{Binding ElementName=WorkArea, Path=Content}"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Width="250" Height="250" Margin="0,0,10,10" Grid.Row="1"/>
Convert
方法在应用程序开始时只调用一次。有趣的是,如果我用返回值
替换返回画布
,它的工作原理就像没有转换器一样,就像在返回之前跳过代码一样。
为什么转换器不工作?我能做什么?“返回值
的工作原理与没有转换器的情况类似”。显然,这是因为您返回了输入值而忽略了新画布。如果您返回值或workAreaCanvas,则不会发生任何明显的变化,因为您没有对workAreaCanvas进行任何更改。您从未解释过“不工作”的确切含义。您是否有任何异常?@PaviełKraskoŭski它仅在WorkArea.Content属性更改时更新(假设它实现了INotifyPropertyChanged)。您可能应该使用与Workarea相同的方式制作navigator,而不是使用转换器。我也不确定在绑定转换器中创建视觉效果是否是一个好主意(至少我以前从未见过),但您必须注意返回的画布是否完成了布局。在从转换器返回之前,调用其Measure
和Arrange
方法。
<local:Navigator DataContext="{Binding ElementName=WorkArea, Path=Content}"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Width="250" Height="250" Margin="0,0,10,10" Grid.Row="1"/>
<UserControl x:Class="DbCreator.Navigator"
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:local="clr-namespace:DbCreator"
mc:Ignorable="d" Opacity="0.9"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<local:WorkAreaToNavigatorConverter x:Key="WorkAreaToNavigatorConverter"/>
</UserControl.Resources>
<Border BorderBrush="#757575" BorderThickness="1"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Width="{Binding ElementName=ViewBox, Path=ActualWidth}"
Height="{Binding ElementName=ViewBox, Path=ActualHeight}">
<Viewbox x:Name="ViewBox" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=ActualHeight}">
<Grid Background="White">
<Rectangle Width="{Binding Content.ActualWidth}"
Height="{Binding Content.ActualHeight}"
Name="Thumbnail">
<Rectangle.Fill>
<VisualBrush Visual="{Binding Content, Converter={StaticResource WorkAreaToNavigatorConverter}}"/>
</Rectangle.Fill>
</Rectangle>
<Border Background="#20000000" x:Name="ViewPort" Cursor="SizeAll"
Width="{Binding ViewportWidth}" Height="{Binding ViewportHeight}"
MaxWidth="{Binding Content.ActualWidth}" MaxHeight="{Binding Content.ActualHeight}"
HorizontalAlignment="Left" VerticalAlignment="Top"
MouseDown="Border_MouseDown"
MouseUp="Border_MouseUp"
MouseMove="Border_MouseMove">
<Border.RenderTransform>
<TranslateTransform X="{Binding HorizontalOffset}" Y="{Binding VerticalOffset}"/>
</Border.RenderTransform>
</Border>
</Grid>
</Viewbox>
</Border>
public class WorkAreaToNavigatorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Canvas workAreaCanvas = value as Canvas;
Canvas canvas = new Canvas()
{
Width = workAreaCanvas.ActualWidth,
Height = workAreaCanvas.ActualHeight,
Background = new SolidColorBrush(Colors.White)
};
foreach (Table table in workAreaCanvas.Children.OfType<Table>())
{
Rectangle rectangle = new Rectangle()
{
Width = table.ActualWidth,
Height = table.ActualHeight,
Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString((string)table.Tag))
};
Canvas.SetLeft(rectangle, Canvas.GetLeft(table));
Canvas.SetTop(rectangle, Canvas.GetTop(table));
Canvas.SetZIndex(rectangle, Canvas.GetZIndex(table));
canvas.Children.Add(rectangle);
}
return canvas;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}