【问题标题】:Drop Event Listbox拖放事件列表框
【发布时间】:2010-09-15 15:22:35
【问题描述】:

我正在尝试将我的文本文件的内容添加到列表框。这是代码:-

这是我的 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 事件在我的网格上不起作用?它接受文本文件,但命令不会触发,我也没有收到任何消息框。

提前致谢:)

【问题讨论】:

    标签: silverlight-4.0 mvvm-light


    【解决方案1】:

    很难说出您的命令为什么不起作用。您应该首先调查您的命令是否正确绑定。

    首先,运行您的应用并观察输出窗口中的任何错误消息。此外,您可以尝试添加带有断点的测试值转换器。

    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>
    

    如果您的命令是绑定的,那么您的断点将被激活。如果你没有得到你的断点,那会提示你还有其他问题。

    【讨论】:

    • - 我终于发现问题是我将RelayCommand 属性声明为私有:p。将其公开并且有效:)
    猜你喜欢
    • 2016-11-03
    • 2012-07-27
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多