【问题标题】:Mvvm Windows Phone 8 command Error: BindingExpression path error: 'SalvarCommand'Mvvm Windows Phone 8 命令错误:BindingExpression 路径错误:'SalvarCommand'
【发布时间】:2013-05-14 11:53:42
【问题描述】:

我的命令指向 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 中寻找我的命令。谁能帮帮我?

【问题讨论】:

    标签: mvvm windows-phone-8 command mvvm-light


    【解决方案1】:

    您可以命名 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      相关资源
      最近更新 更多