Specify custom XAML converter

I have a converter class in a Windows Store app:

namespace MyNamespace {
  public class ColorToBrushConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, string language) {
      if (value is Windows.UI.Color) {
        Windows.UI.Color color = (Windows.UI.Color) value;
        SolidColorBrush r = new SolidColorBrush(color);
        return r;
      }
      CommonDebug.BreakPoint("Invalid input to ColorToBrushConverter");
      throw new InvalidOperationException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) {
      throw new NotImplementedException();
    }
  }
}

Now I am trying to use it in xaml. I can not understand the correct syntax for xaml to tell him to use my converter.

  <ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem" >
      <Setter Property="Background" Value="{Binding Source=BackgroundColor, UpdateSourceTrigger=PropertyChanged, Converter=????????????????}"/>
    </Style>
  </ListView.ItemContainerStyle>

EDIT: Apparently, the Windows Store apps do not allow the developer to use all the data bindings that work in WPF. This seems to explain part of my problem. But I'm still not sure if this will continue after the Windows 8.1 update.

+4
source share
1 answer

- , . XML, ( , , ). :

<Window x:Class="....etc..."

        xmlns:Converters="clr-namespace:MyNamespace;[assembly=the assembly the namespace is in]"
        />

<Window.Resources>
    <Converters:ColorToBrushConverter x:Key="MyColorToBrushConverter" />
</Window.Resources>

<Grid>
    <ListView>
        [snip]
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem" >
                <Setter Property="Background" 
                        Value="{Binding Path=BackgroundColor, 
                                        UpdateSourceTrigger=PropertyChanged, 
                                        Converter={StaticResource MyColorToBrushConverter}
                               }"
                        />
            </Style>
        </ListView.ItemContainerStyle>
        [snip]
+4

Source: https://habr.com/ru/post/1536282/


All Articles