Pukaai
Pukaai

Reputation: 377

Listbox control template object is ignoring size properties

I am pretty much a beginner in WPF C# programming, so sorry for stupid question's.

DESCRIPTION: In the ListBox I have defined the ControlTemplate. Inside of it is located also the "PieDiagram" object. The "PieDiagram" object have an property "Size" which is always ignored - when I set for example "Size" on the value 50, nothing change. I also try to set any other "childs" of the ListBox.Template to the fixed size, but then I get a problem that the whole Content is changed/rescaled (together with text and diagram lines for setting the amounts - look attaced picture).

GOAL: What I want is to change the size of the "PieDiagram" object only - so the Pie must be smaller that the lines ends and the set arrows.

In case of the any additional question needed to understand the problem, please write into the comment section. Thanks!

CODE in View:

<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>

CODE in UserControl "MultiPieChart":

<UserControl
    ...>
    <Control.Template>
        <ControlTemplate TargetType="{x:Type pie:MultiPieChart}">
            <Image x:Name="PART_PieChart" RenderOptions.BitmapScalingMode="NearestNeighbor" />
        </ControlTemplate>
    </Control.Template>
</UserControl>

CODE in UserControl Code behind:

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;
           ...
        }
    }
}

Hows looks now - lines are smaller than a pie diagram

Upvotes: 1

Views: 51

Answers (0)

Related Questions