我正在尝试将文本文件的内容添加到列表框中。这是代码: -
这是我的MainPage.xaml: -
<UserControl
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:ec="http://schemas.microsoft.com/expression/2010/controls" x:Class="MVVMDemo.MainPage"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
mc:Ignorable="d"
Height="300"
Width="300"
>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="DataTemplate1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.579*"/>
<ColumnDefinition Width="0.421*"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="0,0,0,4" TextWrapping="Wrap" Text="{Binding ContactName}" d:LayoutOverrides="Width, Height"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" AllowDrop="True" UseLayoutRounding="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<cmd:EventToCommand Command="{Binding DragCustomerCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox x:Name="lstCustomersName" Margin="8" ItemsSource="{Binding Customers}" ItemTemplate="{StaticResource DataTemplate1}" FontSize="24" FontFamily="Arial Unicode MS" FontWeight="Bold">
<ListBox.Effect>
<DropShadowEffect/>
</ListBox.Effect>
</ListBox>
</Grid>
</UserControl>
这是MainViewModel.cs: -
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using System.IO;
namespace MVVMDemo.ViewModel
{
public class MainViewModel : ViewModelBase
{
private RelayCommand<DragEventArgs> dragCustomerCommand = null;
private RelayCommand<DragEventArgs> DragCustomerCommand
{
get
{
return dragCustomerCommand;
}
set
{
dragCustomerCommand = value;
}
}
public string Welcome
{
get
{
return "Welcome to MVVM Light!!!This is my firstDemo";
}
}
private ObservableCollection<CustomersServiceRef.Customer> customers;
public ObservableCollection<CustomersServiceRef.Customer> Customers
{
get
{
return customers;
}
set
{
customers = value;
RaisePropertyChanged("Customers");
}
}
public MainViewModel()
{
dragCustomerCommand = new RelayCommand<DragEventArgs>(e => {
MessageBox.Show(e.Data.ToString());
if (e.Data == null)
return;
var files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
var file = files[0];
using (var stream = file.OpenRead())
{
using(var reader = new StreamReader(stream)){
var Customer = new CustomersServiceRef.Customer() { ContactName = reader.ReadLine()};
Customers.Insert(0, Customer);
}
}
});
var CustomerService = new CustomersServiceRef.CustomersServiceClient();
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
CustomerService.GetCustomersCompleted += (s, e) =>
{
if (e.Error == null)
{
Customers = new ObservableCollection<CustomersServiceRef.Customer>(e.Result);
}
};
CustomerService.GetCustomersAsync();
}
}
}
}
为什么drop事件在我的网格上不起作用?它接受文本文件,但命令不会触发,我也没有得到任何消息框。
提前致谢:)
答案 0 :(得分:0)
很难说出你的命令无法正常工作的原因。您应首先调查您的命令是否正确绑定。
首先,运行您的应用并观看“输出”窗口以查找任何错误消息。此外,您可以尝试添加带断点的测试值转换器。
public class TestConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.WriteLine("TestConverter.Convert(value := {0}, targetType := {1}, parameter := {2}, culture := {3})",
value, targetType, parameter, culture);
return value; // Put Breakpoint Here!!
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.WriteLine("TestConverter.ConvertBack(value := {0}, targetType := {1}, parameter := {2}, culture := {3})",
value, targetType, parameter, culture);
return value;
}
}
将上述类添加到项目中,在指示的行上放置断点,然后修改MainPage.xaml以使用值转换器。
<UserControl ...
xmlns:yourapp="clr-namespace:YourApplicationNamespace"
>
<UserControl.Resources>
<ResourceDictionary>
<!-- other resource dict stuff removed for brevity -->
<yourapp:TestConverter x:Key="_TestConverter" />
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" AllowDrop="True" UseLayoutRounding="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<cmd:EventToCommand Command="{Binding Path=DragCustomerCommand,
Converter={StaticResource _TestConverter}}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox x:Name="lstCustomersName" Margin="8" ItemsSource="{Binding Customers}" ItemTemplate="{StaticResource DataTemplate1}" FontSize="24" FontFamily="Arial Unicode MS" FontWeight="Bold">
<ListBox.Effect>
<DropShadowEffect/>
</ListBox.Effect>
</ListBox>
</Grid>
</UserControl>
如果您的命令是绑定的,那么您的断点将被激活。如果你没有得到你的断点,那就会让你知道其他错误。