Visual Studio的语言设置损害了Double in Binding

时间:2019-04-07 19:52:35

标签: c# wpf xaml

我创建了一些带有倾斜边缘的“标签”控件,如下所示:

Example tab

倾斜的边缘基本上只是三角形(Polygon)。

为了保持可调整性,我在ResourceDictionary中定义标签高度边缘角度

<!-- Height of the Ribbon Tabs in pixels -->
<system:Int32 x:Key="RibbonTabHeight">36</system:Int32>

<!-- Angle of the Ribbon Tabs edges in degrees -->
<system:Double x:Key="RibbonTabEdgeAngle">60.0</system:Double>

边缘的宽度(即三角形的宽度)将通过三角学计算得出。为此,我实现了一个转换器,因此以后可以在XAML中使用它:

/// <summary>
/// Converts the height of a tab to the width of the sloped edge while retaining the desired angle of the edge
/// </summary>
/// <remarks>
/// The tab height shall be passed as the value to be converted
/// The edge angle shall be passed as the converter parameter
/// </remarks>
public class TabHeightToEdgeWidthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Interpret the passed value as the tab height
        double tabHeight = System.Convert.ToDouble(value);

        // Interpret the passed parameter as the desired angle of the edge
        double edgeAngle = System.Convert.ToDouble(parameter);

        // Convert degrees in radians
        edgeAngle = (Math.PI / 180) * edgeAngle;

        // The tangent of the edge angle equals the opposite side (tab height) divided by the adjacent side (edge width)
        // Therefore, the edge width equals the tab height divided by the tangent of the edge angle
        double edgeWidth = tabHeight / Math.Tan(edgeAngle);

        return edgeWidth;
    }

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

对于标签的结构,我使用的是Grid,其中包含三列-第一列包含左边缘(三角形),第二列包含标签的实际内容(例如图标),第三列包含右边缘(三角形)。

将到目前为止的所有内容放在一起,我有这个:

<Grid x:Name="RibbonTabGrid"
      Height="{Binding Source={StaticResource RibbonTabHeight}}">

    <Grid.ColumnDefinitions>

        <!-- Column 0: Left edge -->
        <ColumnDefinition x:Name="RibbonTabLeftEdgeGridCell"
                          Width="{Binding Source={StaticResource RibbonTabHeight},
                          Converter={StaticResource TabHeightToEdgeWidthConverter},
                          ConverterParameter={StaticResource RibbonTabEdgeAngle}}"/>

        <!-- ... -->

    </Grid.ColumnDefinitions>

</Grid>

但是,一旦我将ConverterParameter添加到上面所示的绑定中,Visual Studio就会告诉我:

“输入字符串的格式不正确”

但是一切都可以构建并正常运行。

这是一个有趣的部分:如果我将ResourceDictionary中的边角定义从Double更改为Int32并去除了小数部分(“ 60”而不是“ 60.0”),那么我不会收到“格式错误”错误。

我真的不知道我在做什么错...

如果我像这样使用双硬编码作为转换器参数...

<!-- Column 0: Left edge -->
<ColumnDefinition x:Name="RibbonTabLeftEdgeGridCell"
                  Width="{Binding Source={StaticResource RibbonTabHeight},
                  Converter={StaticResource TabHeightToEdgeWidthConverter},
                  ConverterParameter='60.0'}"/>

...我也不会收到任何错误。因此,显然在整个转换过程中出了点问题。

有人知道什么可能是错的吗?我可以轻松地使用Int32方法,而不必理会这一点,但是我很想知道它是如何正确完成的...

让我知道我的问题描述是否令人费解,或者是否需要更多信息。对所有其他背景感到抱歉,我认为最好覆盖整个定义-转换-绑定链。

非常感谢您!

编辑:此问题似乎是由Visual Studio的语言设置引起的。我一直在使用Visual Studio,将语言设置为“德语”。在德语中,使用小数逗号代替小数点-但是,在编程中,仍将小数点用作常用语法。当涉及诸如绑定之类的内部类型强制转换时,Visual Studio似乎混淆了很多东西。切换到“英语”后,问题不再出现。我要向微软报告。感谢@PavelAnikhouski的提示!

0 个答案:

没有答案
相关问题