MultiBinding返回一个值,但该值未在width属性中设置 [英] MultiBinding returns a value but the value is not set in the width property

查看:87
本文介绍了MultiBinding返回一个值,但该值未在width属性中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一段代码示例,假设从转换器设置矩形的宽度。我可以看到转换器被调用两次(第二次带有所需的值)但是该值似乎没有达到矩形的Width属性。



注意:我故意使用矩形而不是进度条 - 要求



这是xaml



Hi all

I have a piece of code sample which suppose to set the width of the rectangle from a converter. I can see the converter is called twice (the second time with the required value) but the value does not seem to reach the Width property of the rectangle.

note: I use rectangle on purpose instead of a progressbar - requirements

Here is the xaml

<Window.Resources>
    <viewmodel:CalcWidthFromPercentageConverter x:Key="CalcWidthFromPercentageConverter" />
</Window.Resources>
<Window.DataContext>
    <viewmodel:ViewModel/>
</Window.DataContext>
<Grid>
    <Border x:Name="borderParent" Height="20" HorizontalAlignment="Stretch" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Center">
        <Rectangle Fill="Red" Height="20" HorizontalAlignment="Left">
            <Rectangle.Width>
                <MultiBinding Converter="{StaticResource CalcWidthFromPercentageConverter}">
                    <Binding Path="ProgressWidthPercentage"/>
                    <Binding Path="ActualWidth" ElementName="borderParent"></Binding>
                </MultiBinding>
            </Rectangle.Width>
        </Rectangle>
    </Border>
</Grid>







这里是转换器



公共类CalcWidthFromPercentageConverter:IMultiValueConverter

{

公共对象转换(object []值,类型targetType,对象参数,CultureInfo文化)

{

if(values.Count()== 2)

{

十进制inputPercentage =(十进制)值[0];

十进制borderWidth = System.Convert.ToDecimal(values [1]);

十进制结果= borderWidth * inputPercentage;

返回结果;

}

返回值;

}



公共对象[] ConvertBack(对象值,类型[] targetTypes,对象参数,CultureInfo文化)

{

返回new [] {value};

}

}



并持续观看模型



公共类ViewModel

{

私人只读十进制_progressWidthPercentage;



公共小数ProgressWidthPercentage

{

get {return _progressWidthPercentage; }

}



公共ViewModel()

{

_progressWidthPercentage =(十进制)0.37;

}

}




here is the converter

public class CalcWidthFromPercentageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Count() == 2)
{
decimal inputPercentage = (decimal)values[0];
decimal borderWidth = System.Convert.ToDecimal(values[1]);
decimal result = borderWidth * inputPercentage;
return result;
}
return values;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new[] { value };
}
}

and last the viewmodel

public class ViewModel
{
private readonly decimal _progressWidthPercentage;

public decimal ProgressWidthPercentage
{
get { return _progressWidthPercentage; }
}

public ViewModel()
{
_progressWidthPercentage = (decimal)0.37;
}
}

推荐答案

就我所知,WPF不使用小数任何内部价值观。很多时候甚至要小心使用double,因为还有其他属性,比如Size或Thickness。
WPF does not use decimal as far as I know for any internal values. Many times have to be careful about even using double since there are other properties that are used, like Size or Thickness.


看来我只需要将绑定属性和转换器翻倍而不是十进制。
It appears I just had to turn the binding property and the converter to double instead of decimal.


这篇关于MultiBinding返回一个值,但该值未在width属性中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆