0

我的命令指向 Model 类而不是 ModelView

System.Windows.Data 错误:BindingExpression 路径错误:在“SuperListaW8.Model.ListaItem”“SuperListaW8.Model.ListaItem”上找不到“SalvarCommand”属性

EditListaPage.xaml

<phone:PhoneApplicationPage
x:Class="SuperListaW8.View.EditListaPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wm="clr-namespace:System.Windows.Controls;assembly=WindowsPhoneWatermarkTextBoxControl"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
DataContext="{Binding Source={StaticResource Locator}, Path=ListaViewModel}" 
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

EditListaPage.xaml --> 块文本

<TextBox Grid.Column="1" Background="#f3f3f3" BorderBrush="#f3f3f3" VerticalAlignment="top">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="TextChanged">
                                                <cmd:EventToCommand Command="{Binding Path=SalvarCommand, Mode=TwoWay}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </TextBox>

ViewModelLocator.cs

namespace SuperListaW8.ViewModel
{
public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (!SimpleIoc.Default.IsRegistered<IDataService>())
        {
            SimpleIoc.Default.Register<IDataService, DataService>();
            //SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());            
        }

        SimpleIoc.Default.Register<ListaViewModel>();
    }

    public ListaViewModel ListaViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<ListaViewModel>();
        }
    }

    public static void Cleanup() { }
}
}

ListaViewModel.cs

namespace SuperListaW8.ViewModel
{
   public class ListaViewModel : ViewModelBase
   {
        public RelayCommand SalvarCommand { get; private set; }

    public ListaViewModel(IDataService dataService)
    {
        _dataService = dataService;

        SalvarCommand = new RelayCommand(() =>
        {
            System.Diagnostics.Debugger.Break();    
        });
    }
    }
 }

我的命令是在模型中而不是在 ViewModel ListaItem ListaViewModel 中寻找的。谁能帮我?

4

1 回答 1

0

您可以命名 ListBox,并在 Binding 中使用 ElementName 引用它,在 Path 中使用 DataContext.DetailsVisibility

<ListBox x:Name="listBox" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
           <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Title}" />
            <TextBlock Text="{Binding Details}"
                       Visibility="{Binding ElementName=listBox,
                                            Path=DataContext.DetailsVisibilty}" />
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

于 2013-05-20T17:06:30.917 回答