[英]Window.Resource not recognized in wpf aplication
我是WPF和MVVM模式的新手,我有一个包含2个ComboBox,1个Datapicker,1个文本框和1button的视图,目的是当我按下按钮时,从ViewModel的该视图中获取数据,尝试一下一直基于StackOverflow中的这个问题: https://stackoverflow.com/questions/27447042/xaml-button-comand-to-pass-to-date-picker-properties-to-method
://stackoverflow.com/questions/27447042/xaml-button-comand-to-pass-to-date-picker-properties-to-method
问题是在XAML中
Window.Resources PedidosRetraso:ICommandMultiDateConverter x:Key =“ multiDateConverter” />
给我一个错误,没有定义NameSpace前缀“ PedidosRetraso”,我不知道为什么,名称空间的名称正确,这是我做错了吗?
我也想知道如何验证ComboBox是否具有某些值以在未选择值的情况下禁用按钮,我想我应该在CanExecute中做到这一点,但我不知道我如何知道ComboBox的值。
这是我的XAML代码
<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>
这是我的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)
{
}
}
谢谢。
您已经在xmlns:local="clr-namespace:PedidosRetraso"
定义了名称xmlns:local="clr-namespace:PedidosRetraso"
,请使用它!
更改
<PedidosRetraso:ICommandMultiDateConverter x:Key="multiDateConverter"/>
至
<local:ICommandMultiDateConverter x:Key="multiDateConverter"/>
编辑
您已添加图片。 我认为ICommandMultiDataConverter
不在命名空间PedidosRetraso
,但在PedidosRetraso.viewModel
将xmlns:local="clr-namespace:PedidosRetraso"
更改为xmlns:local="clr-namespace:PedidosRetraso.viewModel"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.