我几乎是 WPF C# 编程的初学者,对于愚蠢的问题,我深表歉意。
描述:在 ListBox 中,我定义了 ControlTemplate。在其中也有“PieDiagram”对象。“PieDiagram”对象有一个属性“Size”,它总是被忽略 - 例如,当我在值 50 上设置“Size”时,没有任何变化。我还尝试将 ListBox.Template 的任何其他“子项”设置为固定大小,但随后我遇到一个问题,即整个内容已更改/重新缩放(连同用于设置数量的文本和图表行 - 看图片) .
目标:我想要的只是改变“PieDiagram”对象的大小——所以饼必须比线结束和设置的箭头更小。
如果需要了解问题的任何其他问题,请写在评论部分。谢谢!
代码在视图中:
<Grid>
<!-- Diagram -->
<ListBox
x:Name="LstBox"
PreviewMouseRightButtonDown="ListBox_PreviewMouseRightButtonDown"
RenderTransformOrigin="0.5,0.5">
<ListBox.LayoutTransform>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5" />
</ListBox.LayoutTransform>
<ListBox.RenderTransform>
<ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1" ScaleY="1" />
</ListBox.RenderTransform>
<ListBox.Template>
<ControlTemplate>
<Border>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Border>
<Border.Background>
<VisualBrush>
<VisualBrush.Visual>
<pie:MultiPieChart
x:Name="chart1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
DataBrushes="{Binding DataBrushes, UpdateSourceTrigger=PropertyChanged}"
DataList="{Binding PieDataCol, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Size="50">
<pie:MultiPieChart.LayoutTransform>
<RotateTransform Angle="180" CenterX="0.5" CenterY="0.5" />
</pie:MultiPieChart.LayoutTransform>
<pie:MultiPieChart.RenderTransform>
<ScaleTransform CenterX="0.5" CenterY="0.5" />
</pie:MultiPieChart.RenderTransform>
</pie:MultiPieChart>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
<ItemsPresenter />
</Border>
</ScrollViewer>
</Border>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemsSource>
<StaticResource ResourceKey="DiagramCol" />
</ListBox.ItemsSource>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Width="{Binding AreaWidth}"
Height="{Binding AreaHeight}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsItemsHost="True">
<Canvas.RenderTransform>
<TranslateTransform X="{Binding HalfAreaWidth}" Y="{Binding HalfAreaHeight}" />
</Canvas.RenderTransform>
</Canvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Bottom" Value="{Binding Y}" />
<Setter Property="Canvas.ZIndex" Value="{Binding ZindexBinding}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter x:Name="Content" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
用户控件“MultiPieChart”中的代码:
<UserControl
...>
<Control.Template>
<ControlTemplate TargetType="{x:Type pie:MultiPieChart}">
<Image x:Name="PART_PieChart" RenderOptions.BitmapScalingMode="NearestNeighbor" />
</ControlTemplate>
</Control.Template>
</UserControl>
CODE 在 UserControl 后面的代码:
private Image _pieChartImage;
public static readonly DependencyProperty SizeProperty = DependencyProperty.Register("Size",
typeof(double), typeof(MultiPieChart), new PropertyMetadata(100.0, OnPiePropertyChanged));
public double Size
{
get { return (double)GetValue(SizeProperty); }
set { SetValue(SizeProperty, value); }
}
private void CreatePieChart()
{
if (_pieChartImage != null)
{
if (!double.IsNaN(Size) && DataList != null && DataList.Any())
{
_pieChartImage.Width = _pieChartImage.Height = Width = Height = Size;
...
}
}
}