Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在BorderThinkness中使用StringFormat绑定int属性_C#_Wpf_Templates_String Formatting - Fatal编程技术网

C# 在BorderThinkness中使用StringFormat绑定int属性

C# 在BorderThinkness中使用StringFormat绑定int属性,c#,wpf,templates,string-formatting,C#,Wpf,Templates,String Formatting,我有一个具有自己属性BorderWidth的模板: public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register("BorderWidth", typeof(int), typeof(ProjectOverviewSensor), new PropertyMetadata(1)); 现在,我想在模板中使用StringFormat将此属性绑定到BorderThickness,

我有一个具有自己属性BorderWidth的模板:

public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register("BorderWidth", typeof(int), typeof(ProjectOverviewSensor), new PropertyMetadata(1));
现在,我想在模板中使用StringFormat将此属性绑定到BorderThickness,以创建特定边的边界。但它在所有四个方面都是1

<Border BorderThickness="{Binding Path=BorderWidth, RelativeSource={RelativeSource TemplatedParent}, StringFormat='{}{0},0,{0},0'}"></Border>
我如何才能将财产仅绑定到我想要的边界控制的两侧?是否有stringformat的替代品?

Binding.stringformat仅适用于System.String类型的属性

对于用XAML编写的字符串,情况就不同了,因为在这里,XAML解析器在解析过程中将字符串转换为厚度值

要执行所需操作,您需要绑定上的转换器:

class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value == DependencyProperty.UnsetValue)
            return value;
        return new Thickness((int)value,0, (int)value, 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((Thickness)value).Left;
    }   
}
然后像这样使用:

<Window.Resources>
    <MyConverter x:Key="ToThickness"/>
</Window.Resources>
<Border BorderThickness="{Binding Path=BorderWidth, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ToThickness}"></Border>

现在我创建了一个扩展转换器,我希望他不要对你隐瞒

public class CustomThicknessConverter : IValueConverter
{
    protected readonly char SplitChar = ',';
    protected readonly char ReplaceAt = '#';

    /// <summary>
    /// Create a thickness with custom format(for example #,2,#,5).
    /// </summary>
    /// Number for default width.
    /// # for given border width.
    /// ; for split the chars.
    /// For example '#,0,#,0' with 1 as value return a new thickness 
    /// with Left = 1, Top = 0, Right = 1, Bottom = 0.
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value == DependencyProperty.UnsetValue)
            return value;

        var borderWidth = value as double?;
        var format = parameter.ToString().Split(SplitChar); 

        if(format.Length == 1)
            return new Thickness(GetValue(borderWidth, format[0]));

        else if(format.Length == 2)
        {
            return new Thickness()
            {
                Left    = GetValue(borderWidth, format[0]),
                Top     = GetValue(borderWidth, format[1]),
                Right   = GetValue(borderWidth, format[0]),
                Bottom  = GetValue(borderWidth, format[1])
            };
        }

        else if(format.Length == 4)
        {
            return new Thickness()
            {
                Left    = GetValue(borderWidth, format[0]),
                Top     = GetValue(borderWidth, format[1]),
                Right   = GetValue(borderWidth, format[2]),
                Bottom  = GetValue(borderWidth, format[3])
            };
        }

        return new Thickness(0);
    }

    private double GetValue(double? value, string format)
    {
        if(format.FirstOrDefault() == ReplaceAt) return value ?? 0;

        double result;
        return (Double.TryParse(format, out result)) ? result : 0;
    }

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

<local:CustomThicknessConverter x:Key="CustomThicknessConverter" />    
<Border BorderThickness="{Binding BorderWidth, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource CustomThicknessConverter}, ConverterParameter='0,#,0,#'}">

</Border>

将BorderWidth的类型更改为厚度是一种替代方法吗?a有一个具有多个边框的模板,这些边框具有统一的边框宽度,但所有边框都在不同的侧面接收边框宽度。在我看来,为每个边界创建一个自己的属性是非常糟糕的