Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

ProgressBar not get Proper grid using MultiBinding Converter in .Net Framework 4.0

$
0
0

Hi,

  I have Migrated from .net Framework 3.5 to .net Framework 4.0

After that I am facing the ProgressBar problem.

In my application in .net Framework 4.0 I am using MultiBinding Converter as follows,

In following converter I am not getting the respective Grid in currGrid which is getting me proper in .net Framework 3.5 as currGrid=progressGrid which is expected.

So how I can overcome from this problem in .net Framework 4.0

How I can get currGrid as progressGrid like in .net Framework 3.5

I want to fill that grid as progress value increases as follows,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Globalization;

namespace MultiBindingConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public double _val = 0;
        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i <= 100; i++)
            {
                _val=_val+10;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }

    public class ProgressBarBrushConverter : IMultiValueConverter
    {
        public double _value = 100;
        DispatcherTimer tm;
        Grid currGrid = new Grid();
        Brush myGray = new SolidColorBrush(Color.FromRgb(157, 163, 165));
        Brush myGreen = new SolidColorBrush(Color.FromRgb(124, 241, 183));


        // Methods
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (values != null && values.Length > 2)
                {
                    double value = (double)values[0];
                    bool bIsIndeterminate = (bool)values[1];
                    double maxValue = (double)values[2];
                    double minValue = (double)values[3];currGrid = values[4] as Grid;
                    System.Windows.Visibility visible = (System.Windows.Visibility)values[5];

                    if (visible != System.Windows.Visibility.Visible)
                    {
                        if (tm != null && tm.IsEnabled)
                            tm.Stop();
                    }
                    else
                    {

                        if (bIsIndeterminate)
                        {
                            if (tm == null)
                            {
                                tm = new DispatcherTimer();
                                tm.Tick += new EventHandler(tm_Tick);
                                tm.Interval = new TimeSpan(1000000);
                            }
                            tm.Start();
                        }
                        else
                        {
                            if (tm != null && tm.IsEnabled)
                                tm.Stop();

                            if (maxValue <= 0)
                                maxValue = 100;

                            int SectorNumber = (int)Math.Ceiling(value / ((maxValue - minValue) / 10));

                            for (int i = 0; i < currGrid.Children.Count; i++)
                            {
                                Object myRect = currGrid.Children[i];

                                if (myRect.GetType() == typeof(Rectangle))
                                {
                                    Rectangle rect = myRect as Rectangle;

                                    if (i < SectorNumber)
                                        rect.Fill = myGreen;
                                    else
                                        rect.Fill = myGray;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                if (tm != null && tm.IsEnabled)
                    tm.Stop();

                return Brushes.White;
            }

            return Brushes.White;
        }

        void tm_Tick(object sender, EventArgs e)
        {
            int iGreenIndex = -1;

            for (int i = 0; i < currGrid.Children.Count; i++)
            {
                Object myRect = currGrid.Children[i];

                if (myRect.GetType() == typeof(Rectangle))
                {
                    Rectangle rect = myRect as Rectangle;
                    if (rect.Fill == myGreen)
                    {
                        iGreenIndex = i;
                        rect.Fill = myGray;
                        break;
                    }
                }
            }

            if (iGreenIndex == -1)
                (currGrid.Children[0] as Rectangle).Fill = myGreen;
            else
            {
                if (iGreenIndex + 1 < currGrid.Children.Count)
                {
                    if (currGrid.Children[iGreenIndex + 1].GetType() == typeof(Rectangle))
                        (currGrid.Children[iGreenIndex + 1] as Rectangle).Fill = myGreen;
                }
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

}


<Window x:Class="MultiBindingConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:MultiBindingConverter"><Window.Resources><ResourceDictionary Source="Controls.xaml"/></Window.Resources><StackPanel><Button Content="Progress" Width="72" Height="21" Click="Button_Click"></Button><ProgressBar Width="250" Height="20" Value="10"></ProgressBar></StackPanel></Window>

and Have ResourceDictionary as follows,

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MultiBindingConverter"><Style TargetType="{x:Type ProgressBar}" ><Setter Property="BorderThickness" Value="1"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ProgressBar}" ><Grid x:Name="parent1"><Border x:Name="PART_Track"><Grid x:Name="parent2"><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><!--<ColumnDefinition Width="35"/>--></Grid.ColumnDefinitions><Grid  x:Name="parent"><Grid Grid.Column="0" x:Name="progressGrid"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="0"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="1"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="2"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="3"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="4"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="5"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="6"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="7"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="8"/><Rectangle Stroke="#42B642" StrokeThickness="1" Grid.Column="9"/><Grid.Background><MultiBinding><MultiBinding.Converter><local:ProgressBarBrushConverter /></MultiBinding.Converter><MultiBinding.ConverterParameter>1</MultiBinding.ConverterParameter><Binding Path="Value" RelativeSource="{RelativeSource TemplatedParent}"/><Binding Path="IsIndeterminate" RelativeSource="{RelativeSource TemplatedParent}"/><Binding Path="Maximum" RelativeSource="{RelativeSource TemplatedParent}"/><Binding Path="Minimum" RelativeSource="{RelativeSource TemplatedParent}"/><Binding ElementName="progressGrid" /><Binding Path="Visibility" RelativeSource="{RelativeSource TemplatedParent}"/><!--<Binding ElementName="txtShowValue" />--></MultiBinding></Grid.Background></Grid></Grid></Grid></Border></Grid></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary>


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>