Page 1 of 1

Having trouble getting Converters to work within ItemsControl...

Posted: 10 Nov 2017, 18:39
by vaninadisc
On Unity 2017.1.0f3 and Noesis 2.1.0b4...

I'm having trouble to get Converters to work in an item control. In the code below, I'm just trying to be able to bind the Canvas.Left of the item to something in it's datasource, through a Converter.
<ItemsControl ItemsSource="{Binding PrimaryAvatarBarsToDisplay}">
    <ItemsControl.Resources>
        <dgx:WorldTopToPointConverter x:Key="WorldTopPositionConverter" />
    </ItemsControl.Resources>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Grid.ColumnSpan="2" Grid.RowSpan="2" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <!-- 1. <Setter Property="Canvas.Left" Value="150" /> -->
            <!-- 2. <Setter Property="Canvas.Left" Value="{Binding WorldLeftPosition}" /> -->
            <!-- 3. <Setter Property="Canvas.Left" Value="{Binding WorldLeftPosition, Converter={StaticResource ResourceKey="WorldLeftPositionConverter"} /> -->
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <dgx:PrimaryAvatarHealthBar />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
For the 3 lines that are commented out-
#1 works as expected.
#2 works as expected.
#3 does not work. BUT, I can tell that the converter code is being called, because my (for debugging) converter is:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    Debug.Log("Converting " + value);
    return 200;
}
and the debug message is getting printed. However, it appears as if Canvas.Left is just getting set to 0. It's definitely not at 200.

Any help would be appreciated, not sure what I'm doing wrong. For reference, the converter's goal (not yet implemented) will be to go from the avatar's position in viewport space, to the space that Noesis works in, I guess scaled off the canvas size. I'm more than happy to take suggestions of better ways to do this while I'm here, but I'm going to need converters working like this in an itemscontrol anyhow.

Thanks!

Re: Having trouble getting Converters to work within ItemsControl...

Posted: 10 Nov 2017, 19:01
by sfernandez
I think it is a problem with types. The converter probably expects a float (please check targetType parameter), and you are returning an integer.
We should probably show that as an error in the Unity console.

Could you please try to return 200.0f instead and let me know?

Re: Having trouble getting Converters to work within ItemsControl...

Posted: 10 Nov 2017, 19:29
by vaninadisc
Ah, Yep! That was it. Thank you very much. I see how it's my fault, but yes, an error message for this would be great, it'd be so easy to accidentally not to a final cast at the end even if I was checking the type.

Re: Having trouble getting Converters to work within ItemsControl...

Posted: 10 Nov 2017, 20:03
by sfernandez
yes, an error message for this would be great, it'd be so easy to accidentally not to a final cast at the end even if I was checking the type
Could you please report it in our bugtracker, this way is better to keep track of bugs and missing features.