0

I´m new with WPF and the MVVM pattern, i have a view with 2 ComboBox, 1 Datapicker, 1 textbox and 1button, the intention is when i hit the button i obtain the data from this view in the ViewModel, to try this i've been based on this question in StackOverflow: https://stackoverflow.com/questions/27447042/xaml-button-comand-to-pass-to-date-picker-properties-to-method

The problema is that in the XAML in

Window.Resources PedidosRetraso:ICommandMultiDateConverter x:Key="multiDateConverter"/>

Give me an error, NameSpace prefix "PedidosRetraso" not defined, and i don´t know why, the name of the namespace is correct,¿What i´m doing wrong?.

Also I would like to know how i can verify that the ComboBox have some value selected to disable the button in case of not selected value, i think i shoul do it in CanExecute but i dont know how i know the value of the ComboBox.

This is my XAML code

<Window x:Class="PedidosRetraso.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:PedidosRetraso"
    mc:Ignorable="d"
    Title="MainWindow" Height="700" Width="700"
    >
<Window.Resources>
    <PedidosRetraso:ICommandMultiDateConverter x:Key="multiDateConverter"/>
</Window.Resources>

<Grid>

    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="22,59,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox_SelectionChanged">
    </ComboBox>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="573,66,0,0" VerticalAlignment="Top" Width="75" >
        <Button.CommandParameter>
            <MultiBinding Converter="{StaticResource ResourceKey=multiDateConverter}">
                <Binding ElementName="textBox" Path="Text"></Binding>
                <Binding ElementName="comboBox" Path="Text"></Binding>
                <Binding ElementName="comboBox1" Path="Text"></Binding>
                <Binding ElementName="Fecha" Path="Text"></Binding>
            </MultiBinding>
        </Button.CommandParameter>
        <Button.Command>
            <Binding Path="GetAllActionLogsBetweenDatesCommand"></Binding>
        </Button.Command>
    </Button>
    <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="183,59,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding _combo}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Situación: "></TextBlock>
                    <TextBlock Text="{Binding Path=Nombre}" Width="80"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>

    </ComboBox>

And this is my ViewModel:

public class RelayCommand : ICommand
{
    private Predicate<object> m_canExecute;
    private Action<object> m_execute;

    public RelayCommand(Action<object> execute)
    {
        m_execute = execute;
    }

    public RelayCommand(Predicate<object> canExecute, Action<object> execute)
    {
        m_canExecute = CanExecute;
        m_execute = execute;
    }

    public bool CanExecute(object parameter)
    {
        if (m_canExecute == null)
        {
            return true;
        }

        return m_canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (m_execute != null)
        {
            m_execute(parameter);
        }
    }
}

public class ICommandMultiDateConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new string[] { values[0].ToString(), values[1].ToString(), values[2].ToString(),values[3].ToString() };
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

class ViewModelMain 
{
    public ICommand GetAllActionLogsBetweenDatesCommand { get; set; }

    public ObservableCollection<PocoCombo> _combo { get; set; }
   public ViewModelMain()
    {
        GetAllActionLogsBetweenDatesCommand = new RelayCommand(GetAllActionLogsBetweenDates_Execute);

        _combo = new ObservableCollection<PocoCombo> { new PocoCombo { Id = 20, Nombre = "Enviado" }, new PocoCombo { Id = 25, Nombre = "DIF" }, new PocoCombo { Id = 30, Nombre = "Confirmado" }, new PocoCombo { Id = 40, Nombre = "RP" }, new PocoCombo { Id = 50, Nombre = "Cerrado" }, new PocoCombo { Id = 60, Nombre = "C" } };
   }


  private void GetAllActionLogsBetweenDates_Execute(object parameter)
    {
        try
        {
            var stringList = parameter as string[];

            string proveedor = stringList[0];
            string empresa = stringList[1];
            string situacion = stringList[2];
            DateTime fecha = DateTime.Parse(stringList[3]);
            // Aqui la consulta SQL
        }
        catch (Exception ex)
        {

        }
    }

EDIT i put a photo enter image description here

Thanks.

Ion
  • 549
  • 1
  • 11
  • 25
  • The CLR namespace `PedidosRetraso` is mapped to the XAML namespace `local` by `xmlns:local="clr-namespace:PedidosRetraso"`. So it should read `local:ICommandMultiDateConverter`. – Clemens Oct 19 '17 at 11:49

1 Answers1

2

You have defined the namespace here xmlns:local="clr-namespace:PedidosRetraso", so use it!

Change

<PedidosRetraso:ICommandMultiDateConverter x:Key="multiDateConverter"/>

To

<local:ICommandMultiDateConverter x:Key="multiDateConverter"/>

EDIT

You have added a picture. ICommandMultiDataConverter isn't in namespace PedidosRetraso as I thought but PedidosRetraso.viewModel

Change xmlns:local="clr-namespace:PedidosRetraso" to xmlns:local="clr-namespace:PedidosRetraso.viewModel"

FakeCaleb
  • 982
  • 8
  • 19
  • Thanks for fast answer, sounds logic, but now say ICommandMultiDateConverter not exist in the clr-namespace:PedidosRetraso – Ion Oct 19 '17 at 11:54
  • @Ion Well does `ICommandMultiDataConverter` exist in your project under the namespace of `PedidosRetraso`? – FakeCaleb Oct 19 '17 at 11:56
  • Yes, exist in the VieModelMain.cs, you can see in the middle of the code of VieModel that i put in the question – Ion Oct 19 '17 at 11:57
  • @Ion If you are certain that `ICommandMultiDataConverter` is defined under that namespace. Try to clean and rebuild and even restart Visual Studio (if you're using VS). Also if this code resides in a different assembly you may need `;assembly=[assembly namespace]` when defining the namespace – FakeCaleb Oct 19 '17 at 12:03
  • It´s very extrain, i also do the clean and rebuild, nothing occurs, then try to exit from VS, then open Visual studio and the Project, but the same error, perhaps i need to define the function in other site of my code? – Ion Oct 19 '17 at 12:10
  • @Ion No? Not sure. I cannot replicate your issue. If `ICommandMultiDateCoverter` is in Namespace `PedidosRetraso`. It should be seen fine. There is something else going wrong? – FakeCaleb Oct 19 '17 at 12:26
  • Is the only problema, I put a photo of the screen in the original message to see that the code is in the ViewModelMain.cs, and in the right of the screen can seeViewModelMain.cs is in the folder ViewModel, inside the solution. Perhaps the solution will be start new Project.. – Ion Oct 19 '17 at 12:37
  • Thanks!!!!! I thought that if i put the father they obtain the childrens – Ion Oct 19 '17 at 12:41