Pass variable through Converterparameter

Asked

Viewed 84 times

1

I have the following code snippet:

Visibility = "{Binding TipoDestinatario, Converter={StaticResource conversorTipoDestinatario}, ConverterParameter=}.

You would need to pass a boolean variable through ConverterParameter=, someone would know how to do?

1 answer

1


Your hat, so far I haven’t been able to use Binding for Converterparameter. Since you need to pass a variable, a viable suggestion is to use Multibinding:

XAML

<Label x:Name="situacaoPedidoLabel" Width="200" Margin="10,0" VerticalAlignment="Center" Content="Situação">
    <Label.Visibility>
        <MultiBinding Converter="{StaticResource VisibilidadePedidoConverter}">
            <MultiBinding.Bindings>
                <Binding Path="Pedido.Situacao" />
                <Binding Path="PodeFecharPedido" />
            </MultiBinding.Bindings>
        </MultiBinding>
    </Label.Visibility>
</Label>

Convert

using System;
using System.Windows.Data;

namespace CSharp2.Capitulo10.Wpf.Produtos.Converters
{
    public class VisibilidadePedidoConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                var podeVisualizar = (System.Convert.ToInt32(values[0]) == 1) && System.Convert.ToBoolean(values[1]);

                return podeVisualizar ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
            }
            catch 
            {
                return System.Windows.Visibility.Visible;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("Not implemented");
        }
    }
}
  • how do I pass a value that comes from a method that is in the presenter?

  • Well, an alternative is to be void and directly manipulate the value you need: public void Initialize() { Contact = Loadcontactfromdatabase(); } public Contact Contact { get; set; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.