Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding the width property with a multibinding gives a width of zero

I have a problem binding the "width" property of a border control. When i bind it with a constant, the change is reflected in runtime, but when i bound it with a converter fails, even if the converter return the same value than the constant.

Here is the user control. The problem is with the "Border" object. If i do this, the control works as spected:

 <UserControl.Resources>
        <DataTemplate x:Key="DataTemplateHeat">
            <Border Margin="1,0,0,0" BorderBrush="Black" BorderThickness="1" Width="100">
            </Border>
        </DataTemplate>
    </UserControl.Resources>
    <DockPanel>
        <ItemsControl x:Name="CnvHeats" ItemsSource="{Binding}" ItemTemplate="{StaticResource  DataTemplateHeat}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Background="Red" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DockPanel>
</UserControl>

Espected result

but if i do this, the control fails. The converter executes the correct number of times, and the parameters are ok, but it fails even if i hardcode a return value:

<UserControl.Resources>
        <DataTemplate x:Key="DataTemplateHeat">
            <Border Margin="1,0,0,0" BorderBrush="Black" BorderThickness="1">
                <Border.Width>
                    <MultiBinding Converter="{StaticResource proportionalWidthConverter}" >
                        <Binding Mode="OneWay" Path="GraphProportion"  />
                        <Binding Mode="OneWay" Path="ActualWidth" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=1}"/>
                    </MultiBinding>
                </Border.Width>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
    <DockPanel>
        <ItemsControl x:Name="CnvHeats" ItemsSource="{Binding}" ItemTemplate="{StaticResource  DataTemplateHeat}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Background="Red" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DockPanel>
</UserControl>

and here is the converter:

public class ProportionalWidthConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return 100;
    }

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

Fail control result

Any help is apreciated. Thank you in advance.

like image 878
Ansenagy Avatar asked Sep 07 '25 01:09

Ansenagy


1 Answers

If you run this example in Debug, the Output window in visual studio will show you the following error:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='100' MultiBindingExpression:target element is 'Border' (Name=''); target property is 'Width' (type 'Double')

The error text explains the issue, the value returned by your Converter is not Double. Change your return statement to return 100.0; and see the result.

like image 87
Novitchi S Avatar answered Sep 10 '25 00:09

Novitchi S