简体   繁体   English

如何在按钮宽度中使用多重绑定

[英]How to use multibinding in Button width

I'm trying to use a Multibinding in combination with a converter with a Button control and Width property in XAML but I can't get it to work. 我正在尝试将Multibinding与XAML中具有Button控件和Width属性的转换器结合使用,但无法正常工作。

The converter is: 转换器是:

public class ColumnsToWidthConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return 40;
    }

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

It's hardcoded for 40 now, for testing purposes. 为了测试目的,它已经硬编码了40个。

The XAML definition is: XAML定义为:

<Button
    Height="{Binding ElementName=root,Path=KeyHeight}"
    FontSize="{Binding FontSize}"
    Content="{Binding Display}"
    Command="{Binding ElementName=root, Path=Command}"
    CommandParameter="{Binding}"
    Style="{StaticResource SelectedButton}">
    <Button.Width>
        <MultiBinding Converter="{StaticResource ColumnsToWidthConverter}">
            <Binding Path="Columns"/>
            <Binding Path="KeyHeight" ElementName="root"/>
        </MultiBinding>
    </Button.Width>
</Button>

The button is rendered from a ListView and defined in the ListView.ItemTemplate . 该按钮从ListView呈现,并在ListView.ItemTemplate定义。 When debugging the application, the converter is passed and the value of 40 is returned. 在调试应用程序时,将传递转换器并返回40的值。 The object[] values parameter contains the correct values passed in the MultiBinding paths. object[] values参数包含在MultiBinding路径中传递的正确值。 However, the width of the button is set to its content and not the 40 as in the example above. 但是,按钮的宽度设置为其内容,而不是如上例所示的40。

The ColumnsToWidthConverter is defined in the parent ListView.Resources ColumnsToWidthConverter在父ListView.Resources定义。

<converter:ColumnsToWidthConverter x:Key="ColumnsToWidthConverter"/>

When I remove the MultiBinding and set the Width property to 40 in the XAML definition, the button is rendered correctly. 当我删除MultiBinding并将XAML定义中的Width属性设置为40时,按钮将正确呈现。

The root element is the usercontrol itself and KeyHeight is a DependencyProperty . root元素是usercontrol本身,而KeyHeightDependencyProperty

How do I set the button width using the multibinding? 如何使用多重绑定设置按钮宽度?

The issue does not come from the multibinding but from the converter itself. 问题不在于多重绑定,而在于转换器本身。 When implementing a converter, you are expected to return the same type of value as expected by the control (there's no implicit conversion since you're the one implementing the converter). 在实现转换器时,期望您返回与控件期望值相同的类型(因为您是在实现转换器,所以没有隐式转换)。 In this case, the Width property is a double , so you should return a value of the same type: 在这种情况下, Width属性是double ,因此您应该返回相同类型的值:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    return 40d;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM