调试时WPF渲染大小问题

时间:2017-08-29 18:48:11

标签: c# wpf xaml

我对wpf渲染大小有一个奇怪的问题,试图为单选按钮编写自定义模板,以便它可以按分辨率进行缩放。 在VS XAML预览中看起来应该是这样,但是当我运行应用程序时它会变成香蕉。

这是VS预览:

Preview

应用:

App

因此我可以看到问题在于渲染大小:

Grid render size

Rectangle

我确实尝试过没有网格,高度和宽度设置为自动等等,每次在VS预览中渲染时都没有任何问题,但很快我调试它(​​F5)它就会变成一些巨大的渲染大小值。调整窗口大小没有区别。

XAML代码:

        <Style TargetType="{x:Type RadioButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <BulletDecorator>
                            <BulletDecorator.Bullet>
                                <Border   Background="{x:Null}" x:Name="PART_Border" Width="{Binding ActualHeight, ElementName=ButtonPanel}" Height="{Binding ActualHeight, ElementName=ButtonPanel}"


                                >
                                    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">

                            <Rectangle Margin="{Binding 
                                                Converter={StaticResource PercentageConverter}, 
                                                ElementName=ButtonPanel, 
                                                Path=ActualHeight, 
                                                ConverterParameter=0.03}" 
                                       Fill="{x:Null}" 
                                       RadiusX="{Binding 
                                                Converter={StaticResource PercentageConverter}, 
                                                ElementName=ButtonPanel, 
                                                Path=ActualHeight, 
                                                ConverterParameter=0.10}" 
                                       RadiusY="{Binding 
                                                Converter={StaticResource PercentageConverter}, 
                                                ElementName=ButtonPanel, 
                                                Path=ActualHeight, 
                                                ConverterParameter=0.10}" 
                                       Stroke="#FFE0E000"
                                       StrokeThickness="{Binding 
                                                Converter={StaticResource PercentageConverter}, 
                                                ElementName=ButtonPanel, 
                                                Path=ActualHeight, 
                                                ConverterParameter=0.07}"  
                                       Width="{Binding 
                                                Converter={StaticResource PercentageConverter}, 
                                                ElementName=ButtonPanel, 
                                                Path=ActualHeight, 
                                                ConverterParameter=0.94}"  
                                       Height="{Binding 
                                                Converter={StaticResource PercentageConverter}, 
                                                ElementName=ButtonPanel, 
                                                Path=ActualHeight, 
                                                ConverterParameter=0.94}"  
                                       />

                                    </Grid>
                                </Border>
                            </BulletDecorator.Bullet>
                        </BulletDecorator>


                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="PART_Border" Property="Opacity" Value="1"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter TargetName="PART_Border" Property="Opacity" Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

谢谢

1 个答案:

答案 0 :(得分:0)

问题在于价值转换器,如果有人遇到同样的问题,那就是解决方案:

    public class PercentageConverter : IValueConverter
    {
        public object Convert(object value,
            Type targetType,
            object parameter,
            System.Globalization.CultureInfo culture)
        {

            double _value = Double.Parse(value.ToString(), CultureInfo.CurrentCulture);
            double _parameter = Double.Parse(parameter.ToString(), culture);
            return _value * _parameter;
        }

        public object ConvertBack(object value,
            Type targetType,
            object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }