1

Friends I am using converter for the first time. I want to convert between Bool and String for the Visibility purpose of Button 'Help'. Getter-Setter for HelpVisibility are working but I am not sure why visibility of button is not working. Below is my code. Please help.

VehicalForm.xaml

<Window x:Class="Seris.VehicleForm"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="500" Width="650"
            xmlns:l="clr-namespace:Seris.Converters">
<Window.Resources>
    <l:Bool2String x:Key="converter" />
</Window.Resources>
<Control>

    <Control.Template>

        <ControlTemplate>

            <WrapPanel Orientation="Vertical" Margin="10 " >

                <Label Content="Vehical No" HorizontalAlignment="Left"/>
                <TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicalNo, UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" />

                <Label Content="Model" HorizontalAlignment="Left"/>
                <TextBox Name="Model_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding Model, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" />

                <Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
                <DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="136"/>

                <Label Content="IU No" HorizontalAlignment="Left"/>
                <TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"/>

                <Label Content="Personnel" HorizontalAlignment="Left"/>
                <ComboBox Name="Personnel_Combo" SelectedValue="{Binding PersonnelNameSelected, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding PersonnelName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Width="126"/>

                <Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>

                <Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
                <Button Name="Replace_Button" CommandParameter="replace" IsEnabled="{Binding isEnableReplaceButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Command="{Binding ReplaceButton_Command}" Content="Replace" Width="66"/>
                <Button Name="Remove_Button" CommandParameter="replace" IsEnabled="{Binding isEnableReplaceButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Command="{Binding RemoveButton_Command}" Content="Remove" Width="66"/>


                <Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>

                <ListView Name ="Grid" Height="294" Width="371" >
                    <DataGrid Name="DG" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedRow, Mode=TwoWay}" GridLinesVisibility="None" IsReadOnly="True" AutoGenerateColumns="False" BorderThickness="0">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Vehical No" Binding="{Binding VehicalNo}"/>
                            <DataGridTextColumn Header="Model" Binding="{Binding Model}" />
                            <DataGridTextColumn Header="ManufacturingDate" Binding="{Binding ManufacturingDate}" />
                            <DataGridTextColumn Header="IUNo" Binding="{Binding IUNo}" />
                            <DataGridTextColumn Header="Personnel" Binding="{Binding PersonnelNameSelected}" />
                        </DataGrid.Columns>
                    </DataGrid>
                </ListView>

                <TextBlock Name="Notification" Text="{Binding EditText, UpdateSourceTrigger=PropertyChanged}"/>

                <ProgressBar Name="Progressbar" Minimum="0" Maximum="100" Value="{Binding Progress, UpdateSourceTrigger=PropertyChanged}" Height="11"/>
                <TextBlock Text="{Binding ElementName=Progressbar, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />

                <Button Name="Help" Visibility="{Binding HelpVisibility, Converter={StaticResource converter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" CommandParameter="help" Height="50" Width="50" Click="Help_Click" HorizontalAlignment="Right">
                    <Image Height="45" Width="45" Source="../Images/help.jpg" HorizontalAlignment="Left"/>
                </Button>

            </WrapPanel>

        </ControlTemplate>

    </Control.Template>

</Control>

</Window>

Bool2String.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Seris.Converters
{
class Bool2String : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool)
            return bool.TrueString;
        return null;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value is string)
        {
            if (value == "true")
                return true;
            else if (value == "false")
                return false;
        }
        return null;
    }
}
}

VehicalMainViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seris.Models;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Seris.Commands;
using Seris.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Threading;
using System.ComponentModel;
using Seris.Views;

namespace Seris.ViewModels
{
public class VehicleMainViewModel : ObservableObject
{


    #region Getters-Setters

    // Static Variables

    private static bool _IsEnableReplaceButton;
    public static bool IsEnableReplaceButton
    {
        get { return _IsEnableReplaceButton; }
        set { _IsEnableReplaceButton = value; }
    }

    // Non-Static Variables

    public ObservableCollection<VehicleModel> _listItems;

    public ObservableCollection<VehicleModel> ListItems
    {
        get { return _listItems; }
        set
        {
            if (value == null || (!value.Equals(_listItems)))
            {
                _listItems = value;
            }
        }
    }


    private int _Progress;

    public int Progress
    {
        get { return _Progress; }
        set { _Progress = value; OnPropertyChanged("Progress"); }
    }

    private string _ErroMesage;

    public string ErrorMessage
    {
        get { return _ErroMesage; }
        set 
        { 
            _ErroMesage = value; OnPropertyChanged("ErrorMessage");
            if (ErrorMessage.Trim() == "" || ErrorMessage == null)
                HelpVisibility = false;
            else
                HelpVisibility = true;
        }
    }

    private bool _HelpVisibility;
    public bool HelpVisibility
    {
        get { return _HelpVisibility; }
        set { _HelpVisibility = value; OnPropertyChanged("HelpVisibility"); }
    }


    private Guid? _UniqueNo;

    public Guid? UniqueNo 
    {
        get { return _UniqueNo; }
        set
        {
            if (value == null || (!value.Equals(_UniqueNo)))
            {
                _UniqueNo = value;
                OnPropertyChanged("VehicalNo");

            }
        }
    }

    private string _VehicalNo;

    public string VehicalNo 
    {
        get { return _VehicalNo; }
        set
        {
            if (value == null || (!value.Equals(_VehicalNo)))
            {
                _VehicalNo = value;
                EditText = _VehicalNo;
                OnPropertyChanged("VehicalNo");

            }
        }
    }
    private string _Model;

    public string Model
    {
        get { return _Model; }
        set
        {
            if (value == null || (!value.Equals(_Model)))
            {
                _Model = value;
                EditText = _Model;
                OnPropertyChanged("Model");
            }
        }
    }
    private DateTime? _ManufacturingDate;

    public DateTime? ManufacturingDate
    {
        get { return _ManufacturingDate; }
        set
        {
            if (value == null || (!value.Equals(_ManufacturingDate)))
            {
                _ManufacturingDate = value;
                EditText = _ManufacturingDate.ToString();
                OnPropertyChanged("ManufacturingDate");
            }
        }
    }
    private string _IUNo;

    public string IUNo
    {
        get { return _IUNo; }
        set
        {
            if (value == null || (!value.Equals(_IUNo)))
            {
                _IUNo = value;
                EditText = _IUNo;
                OnPropertyChanged("IUNo");
            }
        }
    }
    private ObservableCollection<string> _PersonnelName;

    public ObservableCollection<string> PersonnelName
    {
        get { return _PersonnelName; }
        set
        {
            if (value != _PersonnelName)
            {
                _PersonnelName = value;
                EditText = _VehicalNo;
                OnPropertyChanged("PersonnelName");
            }
        }
    }

    private string _PersonnelNameSelected;

    public string PersonnelNameSelected
    {
        get { return _PersonnelNameSelected; }
        set
        {
            if (value != _PersonnelNameSelected)
            {
                _PersonnelNameSelected = value;
                EditText = _PersonnelNameSelected;
                OnPropertyChanged("PersonnelNameSelected");
            }
        }
    }

    private string _EditText;

    public string EditText
    {
        get { return _EditText; }
        set
        {
            if (value != _EditText)
            {
                _EditText = value;
                OnPropertyChanged("EditText");
            }
        }
    }

    private VehicleModel _SelectedRow;
    public VehicleModel SelectedRow
    {
        get { return _SelectedRow; }
        set
        {
            if (value != _SelectedRow)
            {
                _SelectedRow = value;
                OnPropertyChanged("SelectedRow");

                if (SelectedRow != null)
                {
                    UniqueNo = _SelectedRow.UniqueNo;
                    VehicalNo = _SelectedRow.VehicalNo;
                    Model = _SelectedRow.Model;
                    ManufacturingDate = _SelectedRow.ManufacturingDate;
                    IUNo = _SelectedRow.IUNo;
                    PersonnelNameSelected = _SelectedRow.PersonnelNameSelected;

                    _IsEnableReplaceButton = true;
                }

            }
        }
    } 
    #endregion

    #region Command Objects
    private ICommand _saveButton_Command;

    public ICommand SaveButton_Command
    {
        get { return _saveButton_Command; }
        set { _saveButton_Command = value; }
    }

    private ICommand _ReplaceButton_Command;

    public ICommand ReplaceButton_Command
    {
        get { return _ReplaceButton_Command; }
        set { _ReplaceButton_Command = value; }
    }

    private ICommand _RemoveButton_Command;

    public ICommand RemoveButton_Command
    {
        get { return _RemoveButton_Command; }
        set { _RemoveButton_Command = value; }
    }
    #endregion

    #region Methods

    //Static Methods



    #region Constructors

    public VehicleMainViewModel()
    {
        // Initialization
        VehicleModel vm = new VehicleModel();
        ListItems = new ObservableCollection<VehicleModel>();
        PersonnelName = ListOfPersonnelViewModel.PersonNameList_Updating;
        //PersonnelName = new ObservableCollection<string>() { "ABC", "DEF", "GHI" };

        // Setting Flags
        ErrorMessage = "";
        IsEnableReplaceButton = false;
        //



        // Commands Initialization
        SaveButton_Command = new RelayCommand(new Action<object>(SaveToList));
        ReplaceButton_Command = new RelayCommand(new Action<object>(ReplaceToList));
        RemoveButton_Command = new RelayCommand(new Action<object>(RemoveList));

    } 

    #endregion


}
}
Pratik
  • 161
  • 4
  • 13
  • Duplicate: [Binding a Button Visibility to bool value in ViewModel](http://stackoverflow.com/questions/7000819/binding-a-button-visibility-to-bool-value-in-viewmodel) – Suresh Aug 05 '14 at 06:04
  • 1
    Hey, your code sample is way too long to read. Could you please post a small reproduce of your problem or trim your code to include the problematic section. – Yuval Itzchakov Aug 05 '14 at 06:04
  • 1
    in your converter you should return Visibilty.Visible or Visibility.Collapsed, not true or false or string – Den Aug 05 '14 at 06:06
  • Oh @Den: So no need of converter? – Pratik Aug 05 '14 at 06:08
  • Anyways, Thanks 2 all... – Pratik Aug 05 '14 at 06:12

2 Answers2

1

In your converter you should return Visibilty.Visible or Visibility.Collapsed, not true or false or string .

Pratik
  • 161
  • 4
  • 13
1

Visibility property accepts value as "Hidden", "Collapse" or "Visible". You need to set these values to Visibility property.

You can refer following link for code to BoolToVisibilityconverter WPF Converter Property

Please dont forget to like answer if you find it useful.

Community
  • 1
  • 1
Krutika
  • 27
  • 1
  • 6