0

I got the following Button Style:

<Style TargetType="Button" 
           BasedOn="{StaticResource {x:Type Control}}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border CornerRadius="4" Background="{TemplateBinding Background}">
                        <Grid>
                            <Path Data="{TemplateBinding Content}" Fill="White" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

There is a Converter which converts some enum value to the wished data string of the path:

public class SugarOperationEnumToSolidImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var sugarOperationEnum = (SugarOperationEnum)value;

        switch (sugarOperationEnum)
        {
            case SugarOperationEnum.Increment:
                return CoffeeMachineIcons.PlusIcon;
            case SugarOperationEnum.Decrement:
                return CoffeeMachineIcons.MinusIcon;
        }

        throw new NotImplementedException();
    }

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

and a button:

<Button Style="{StaticResource SugarButtonStyle}"
                Content="{Binding DecrementSugarButtonContent, Converter={StaticResource SugarOperationEnumToSolidImageConverter}}"/>

For some reason the TemplateBinding of the Content of a button isn't working when I bind it into Data of a Path.

any Ideas how can I overcome this?

7
  • Why can't you try using Style.Triggers to set image as a content instead of IValueConverter?
    – dhilmathy
    Jul 5, 2018 at 15:12
  • Do you get any errors? Binding errors? Or simply nothing is shown ? Jul 5, 2018 at 15:16
  • No errors and Binding errors. Nothing is shown.. it's funny because when I switch the path with: <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center"/> the string of the data is shown Jul 5, 2018 at 15:19
  • @KaliTheGreat but is CoffeeMachineIcons.PlusIcon a geometry or simply a string? Jul 5, 2018 at 15:20
  • @Babbillumpa It's a string... Jul 5, 2018 at 15:20

1 Answer 1

1

You should use Geometry.Parse to bind Data, because Data is a Geometry and not a string, so you could modify your converter like this:

public class SugarOperationEnumToSolidImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var sugarOperationEnum = (SugarOperationEnum)value;

        switch (sugarOperationEnum)
        {
            case SugarOperationEnum.Increment:
                return Geometry.Parse(CoffeeMachineIcons.PlusIcon);
            case SugarOperationEnum.Decrement:
                return Geometry.Parse(CoffeeMachineIcons.MinusIcon);
        }

        throw new NotImplementedException();
    }

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

But consider to store the path as static resource rather parse them every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.