Access IValueConverter From ResourceDictionary

mrw 201 Reputation points
2020-12-03T08:48:49.303+00:00

I have Canvas that is icon:

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:DispatherTimer" >

    <!--CLOCK FOR TIME EXCEEDED-->

    <Style TargetType="{x:Type ContentControl}" x:Key="ClockTimeExceeded_icon">
        <Style.Resources>
            <local:TimeExceededConverter x:Key="TimeExceededConverter"/>
        </Style.Resources>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Viewbox Width="{Binding Width, RelativeSource={RelativeSource AncestorType=ContentControl}}">
                        <Canvas Width="512" Height="512">
                            <Path Fill="black"  Data="F1 M 234.781,197.087 C 234.771,197.087 234.761,197.087 234.751,197.087 C 234.751,219.252 234.925,241.419 234.660,263.580 C 234.562,271.817 237.278,278.238 243.118,284.035 C 277.178,317.851 311.052,351.854 344.998,385.784 C 346.528,387.313 348.047,388.885 349.753,390.202 C 361.450,399.234 378.697,393.771 382.977,379.646 C 385.675,370.744 382.910,363.359 376.486,356.948 C 344.520,325.044 312.625,293.069 280.618,261.206 C 278.189,258.787 277.152,256.385 277.174,252.931 C 277.320,229.600 277.251,206.269 277.250,182.937 C 277.249,164.939 277.196,146.940 277.270,128.942 C 277.298,122.220 275.190,116.453 270.083,112.058 C 263.411,106.316 255.698,105.069 247.681,108.466 C 239.437,111.959 234.876,118.531 234.825,127.593 C 234.693,150.757 234.781,173.922 234.781,197.087 Z M 240.000,0.000 C 250.667,0.000 261.333,0.000 272.000,0.000 C 280.717,1.137 289.485,1.980 298.144,3.461 C 346.223,11.682 388.770,31.768 425.202,64.194 C 469.743,103.837 497.602,153.061 508.070,211.893 C 509.726,221.201 510.707,230.628 512.000,240.000 C 512.000,250.667 512.000,261.333 512.000,272.000 C 511.716,273.285 511.270,274.558 511.170,275.858 C 508.147,315.351 496.828,352.328 476.207,386.114 C 435.574,452.687 376.890,493.618 300.077,508.081 C 290.801,509.827 281.363,510.714 272.000,512.000 C 261.333,512.000 250.667,512.000 240.000,512.000 C 233.755,511.228 227.495,510.559 221.267,509.667 C 170.321,502.370 125.302,481.968 86.819,447.783 C 42.368,408.295 14.608,359.194 3.989,300.599 C 2.273,291.136 1.311,281.537 0.000,272.000 C 0.000,261.333 0.000,250.667 0.000,240.000 C 1.119,231.445 1.957,222.843 3.402,214.343 C 12.458,161.089 35.820,115.014 73.751,76.568 C 112.043,37.756 158.122,13.390 211.909,3.933 C 221.215,2.297 230.633,1.295 240.000,0.000 Z">
                            </Path>
                            <!-- Layer 1/<Group>/<Path> -->
                            <Path Fill="Transparent" Data="F1 M 0.000,272.000 C 1.311,281.537 2.273,291.136 3.989,300.599 C 14.608,359.194 42.368,408.295 86.819,447.783 C 125.302,481.968 170.321,502.370 221.267,509.667 C 227.495,510.559 233.755,511.228 240.000,512.000 C 160.000,512.000 80.000,512.000 0.000,512.000 C 0.000,432.000 0.000,352.000 0.000,272.000 Z"/>

                            <!-- Layer 1/<Group>/<Path> -->
                            <Path Fill="Transparent" Data="F1 M 272.000,512.000 C 281.363,510.714 290.801,509.827 300.077,508.081 C 376.890,493.618 435.574,452.687 476.207,386.114 C 496.828,352.328 508.147,315.351 511.170,275.858 C 511.270,274.558 511.716,273.285 512.000,272.000 C 512.000,352.000 512.000,432.000 512.000,512.000 C 432.000,512.000 352.000,512.000 272.000,512.000 Z"/>

                            <!-- Layer 1/<Group>/<Path> -->
                            <Path Fill="Transparent" Data="F1 M 512.000,240.000 C 510.707,230.628 509.726,221.201 508.070,211.893 C 497.602,153.061 469.743,103.837 425.202,64.194 C 388.770,31.768 346.223,11.682 298.144,3.461 C 289.485,1.980 280.717,1.137 272.000,0.000 C 352.000,0.000 432.000,0.000 512.000,0.000 C 512.000,80.000 512.000,160.000 512.000,240.000 Z"/>

                            <!-- Layer 1/<Group>/<Path> -->
                            <Path Fill="Transparent" Data="F1 M 240.000,0.000 C 230.633,1.295 221.215,2.297 211.909,3.933 C 158.122,13.390 112.043,37.756 73.751,76.568 C 35.820,115.014 12.458,161.089 3.402,214.343 C 1.957,222.843 1.119,231.445 0.000,240.000 C 0.000,160.083 0.000,80.166 0.000,0.000 C 80.000,0.000 160.000,0.000 240.000,0.000 Z"/>

                            <!-- Layer 1/<Group>/<Path> -->
                            <Path Fill="Transparent" Data="F1 M 234.781,197.087 C 234.781,173.922 234.693,150.757 234.825,127.593 C 234.876,118.531 239.437,111.959 247.681,108.466 C 255.698,105.069 263.411,106.316 270.083,112.058 C 275.190,116.453 277.298,122.220 277.270,128.942 C 277.196,146.940 277.249,164.939 277.250,182.937 C 277.251,206.269 277.320,229.600 277.174,252.931 C 277.152,256.385 278.189,258.787 280.618,261.206 C 312.625,293.069 344.520,325.044 376.486,356.948 C 382.910,363.359 385.675,370.744 382.977,379.646 C 378.697,393.771 361.450,399.234 349.753,390.202 C 348.047,388.885 346.528,387.313 344.998,385.784 C 311.052,351.854 277.178,317.851 243.118,284.035 C 237.278,278.238 234.562,271.817 234.660,263.580 C 234.925,241.419 234.751,219.252 234.751,197.087 C 234.761,197.087 234.771,197.087 234.781,197.087 Z"/>

                        </Canvas>
                    </Viewbox>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

I also have IValueConverter

TimeExceededConverter.cs:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace DispatherTimer
{
    class TimeExceededConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string HoursLimitProp = (string)value;

            TimeSpan res;
            var result = TimeSpan.TryParseExact(HoursLimitProp, @"hh\:mm\:ss", CultureInfo.InvariantCulture, out res);

            TimeSpan RingTime = HelperClass.ParseToTimeSpanRingTime();

            if (res > RingTime)
            {
                return Brushes.Red;
            }
            else
            {
                return Brushes.Green;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }

        #endregion
    }
}

MainWindow.xaml:

<Window x:Class="DispatherTimer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DispatherTimer" 
        mc:Ignorable="d"
        Title="Simple timer" Height="450" Width="800" Background="LightGray" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <local:TimeExceededConverter x:Key="TimeExceededConverter"></local:TimeExceededConverter>
    </Window.Resources>


    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label HorizontalAlignment="Center" Margin="0,166,0,132" Width="428" 
               Foreground="{Binding Path=CurrentTime, Converter={StaticResource TimeExceededConverter}}"
               FontSize="70" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
               x:Name="lblTime" Content="{Binding Path=CurrentTime}"/>
        <TextBox x:Name="HoursLimitBox" Text="{Binding HoursLimitProp, Mode=TwoWay}" HorizontalAlignment="Left" Height="36" Margin="133,10,0,0" 
                 VerticalAlignment="Top" Width="166" FontSize="20"/>
        <Label Content="Set limit time:" HorizontalAlignment="Left" Height="36" Margin="10,10,0,0" VerticalAlignment="Top" Width="118" FontSize="18"/>
        <Viewbox Margin="725,8,12,364">
            <ContentControl Style="{StaticResource ClockTimeExceeded_icon}" Foreground="{Binding Path=CurrentTime, Converter={StaticResource TimeExceededConverter}}" Margin="10" />
        </Viewbox>
        <Button Content="Update" HorizontalAlignment="Left" Height="36" Margin="317,10,0,0" VerticalAlignment="Top" Width="97" Click="Button_Click" Background="Black" Foreground="White" FontWeight="Bold">
            <Button.Style>
                <Style TargetType="Button">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ThicknessAnimation Duration="0:0:0.250" To="0" 
                                                            Storyboard.TargetProperty="BorderThickness" />
                                        <DoubleAnimation Duration="0:0:0.550" To="120" 
                                                            Storyboard.TargetProperty="Height" />
                                        <DoubleAnimation Duration="0:0:0.550" To="120" 
                                                            Storyboard.TargetProperty="Width" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ThicknessAnimation Duration="0:0:0.250" To="0" 
                                                            Storyboard.TargetProperty="BorderThickness" />
                                        <DoubleAnimation Duration="0:0:0.550" To="100" 
                                                            Storyboard.TargetProperty="Height" />
                                        <DoubleAnimation Duration="0:0:0.550" To="100" 
                                                            Storyboard.TargetProperty="Width" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>


    </Grid>
</Window>

My question: How I can change clock color based on IValueConverter? I want to apply same logic to Canvas = Icon that I am applying to Label with time at the moment.

Here is link to project:

https://github.com/vadimffe/SimpleTimer

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-12-04T05:12:53.147+00:00

    You need to update your first Path Fill property in ClockTimeExceeded_icon like <Path Fill="{Binding Foreground,RelativeSource={RelativeSource AncestorType=ContentControl}}" ....... > </Path>
    So your ClockTimeExceeded_icon should be like this:

    <Style TargetType="{x:Type ContentControl}" x:Key="ClockTimeExceeded_icon">  
            <Style.Resources>  
                <local:TimeExceededConverter x:Key="TimeExceededConverter"/>  
            </Style.Resources>  
            <Setter Property="ContentTemplate">  
                <Setter.Value>  
                    <DataTemplate>  
                        <Viewbox Width="{Binding Width, RelativeSource={RelativeSource AncestorType=ContentControl}}">  
                            <Canvas Width="512" Height="512">  
      
                                <Path Fill="{Binding Foreground,RelativeSource={RelativeSource AncestorType=ContentControl}}"  Data="F1 M 234.781,197.087 C 234.771,197.087 234.761,197.087 234.751,197.087 C 234.751,219.252 234.925,241.419 234.660,263.580 C 234.562,271.817 237.278,278.238 243.118,284.035 C 277.178,317.851 311.052,351.854 344.998,385.784 C 346.528,387.313 348.047,388.885 349.753,390.202 C 361.450,399.234 378.697,393.771 382.977,379.646 C 385.675,370.744 382.910,363.359 376.486,356.948 C 344.520,325.044 312.625,293.069 280.618,261.206 C 278.189,258.787 277.152,256.385 277.174,252.931 C 277.320,229.600 277.251,206.269 277.250,182.937 C 277.249,164.939 277.196,146.940 277.270,128.942 C 277.298,122.220 275.190,116.453 270.083,112.058 C 263.411,106.316 255.698,105.069 247.681,108.466 C 239.437,111.959 234.876,118.531 234.825,127.593 C 234.693,150.757 234.781,173.922 234.781,197.087 Z M 240.000,0.000 C 250.667,0.000 261.333,0.000 272.000,0.000 C 280.717,1.137 289.485,1.980 298.144,3.461 C 346.223,11.682 388.770,31.768 425.202,64.194 C 469.743,103.837 497.602,153.061 508.070,211.893 C 509.726,221.201 510.707,230.628 512.000,240.000 C 512.000,250.667 512.000,261.333 512.000,272.000 C 511.716,273.285 511.270,274.558 511.170,275.858 C 508.147,315.351 496.828,352.328 476.207,386.114 C 435.574,452.687 376.890,493.618 300.077,508.081 C 290.801,509.827 281.363,510.714 272.000,512.000 C 261.333,512.000 250.667,512.000 240.000,512.000 C 233.755,511.228 227.495,510.559 221.267,509.667 C 170.321,502.370 125.302,481.968 86.819,447.783 C 42.368,408.295 14.608,359.194 3.989,300.599 C 2.273,291.136 1.311,281.537 0.000,272.000 C 0.000,261.333 0.000,250.667 0.000,240.000 C 1.119,231.445 1.957,222.843 3.402,214.343 C 12.458,161.089 35.820,115.014 73.751,76.568 C 112.043,37.756 158.122,13.390 211.909,3.933 C 221.215,2.297 230.633,1.295 240.000,0.000 Z">  
                                </Path>  
                                <!-- Layer 1/<Group>/<Path> -->  
                                <Path Fill="Transparent" Data="F1 M 0.000,272.000 C 1.311,281.537 2.273,291.136 3.989,300.599 C 14.608,359.194 42.368,408.295 86.819,447.783 C 125.302,481.968 170.321,502.370 221.267,509.667 C 227.495,510.559 233.755,511.228 240.000,512.000 C 160.000,512.000 80.000,512.000 0.000,512.000 C 0.000,432.000 0.000,352.000 0.000,272.000 Z"/>  
      
                                <!-- Layer 1/<Group>/<Path> -->  
                                <Path Fill="Transparent" Data="F1 M 272.000,512.000 C 281.363,510.714 290.801,509.827 300.077,508.081 C 376.890,493.618 435.574,452.687 476.207,386.114 C 496.828,352.328 508.147,315.351 511.170,275.858 C 511.270,274.558 511.716,273.285 512.000,272.000 C 512.000,352.000 512.000,432.000 512.000,512.000 C 432.000,512.000 352.000,512.000 272.000,512.000 Z"/>  
      
                                <!-- Layer 1/<Group>/<Path> -->  
                                <Path Fill="Transparent" Data="F1 M 512.000,240.000 C 510.707,230.628 509.726,221.201 508.070,211.893 C 497.602,153.061 469.743,103.837 425.202,64.194 C 388.770,31.768 346.223,11.682 298.144,3.461 C 289.485,1.980 280.717,1.137 272.000,0.000 C 352.000,0.000 432.000,0.000 512.000,0.000 C 512.000,80.000 512.000,160.000 512.000,240.000 Z"/>  
      
                                <!-- Layer 1/<Group>/<Path> -->  
                                <Path Fill="Transparent" Data="F1 M 240.000,0.000 C 230.633,1.295 221.215,2.297 211.909,3.933 C 158.122,13.390 112.043,37.756 73.751,76.568 C 35.820,115.014 12.458,161.089 3.402,214.343 C 1.957,222.843 1.119,231.445 0.000,240.000 C 0.000,160.083 0.000,80.166 0.000,0.000 C 80.000,0.000 160.000,0.000 240.000,0.000 Z"/>  
      
                                <!-- Layer 1/<Group>/<Path> -->  
                                <Path Fill="Transparent" Data="F1 M 234.781,197.087 C 234.781,173.922 234.693,150.757 234.825,127.593 C 234.876,118.531 239.437,111.959 247.681,108.466 C 255.698,105.069 263.411,106.316 270.083,112.058 C 275.190,116.453 277.298,122.220 277.270,128.942 C 277.196,146.940 277.249,164.939 277.250,182.937 C 277.251,206.269 277.320,229.600 277.174,252.931 C 277.152,256.385 278.189,258.787 280.618,261.206 C 312.625,293.069 344.520,325.044 376.486,356.948 C 382.910,363.359 385.675,370.744 382.977,379.646 C 378.697,393.771 361.450,399.234 349.753,390.202 C 348.047,388.885 346.528,387.313 344.998,385.784 C 311.052,351.854 277.178,317.851 243.118,284.035 C 237.278,278.238 234.562,271.817 234.660,263.580 C 234.925,241.419 234.751,219.252 234.751,197.087 C 234.761,197.087 234.771,197.087 234.781,197.087 Z"/>  
      
                            </Canvas>  
                        </Viewbox>  
                    </DataTemplate>  
                </Setter.Value>  
            </Setter>  
        </Style>  
    

    This is my answer based on my understanding, if I misunderstand, please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.