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)
{
}
}
Thanks.