相关疑难解决方法(0)

将listbox项中的命令绑定到viewmodel parent上的属性

我一直在研究这个问题大约一个小时,并查看了所有相关的SO问题.

我的问题很简单:

我有HomePageVieModel:

HomePageVieModel
+IList<NewsItem> AllNewsItems
+ICommand OpenNews
Run Code Online (Sandbox Code Playgroud)

我的加价:

<Window DataContext="{Binding HomePageViewModel../>
<ListBox ItemsSource="{Binding Path=AllNewsItems}">
 <ListBox.ItemTemplate>
   <DataTemplate>
       <StackPanel>
        <TextBlock>
           <Hyperlink Command="{Binding Path=OpenNews}">
               <TextBlock Text="{Binding Path=NewsContent}" />
           </Hyperlink>
        </TextBlock>
      </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

列表显示所有项目都很好,但对于我的生活,无论我尝试命令将无法工作:

<Hyperlink Command="{Binding Path=OpenNewsItem, RelativeSource={RelativeSource AncestorType=vm:HomePageViewModel, AncestorLevel=1}}">
<Hyperlink Command="{Binding Path=OpenNewsItem, RelativeSource={RelativeSource AncestorType=vm:HomePageViewModel,**Mode=FindAncestor}**}">
<Hyperlink Command="{Binding Path=OpenNewsItem, RelativeSource={RelativeSource AncestorType=vm:HomePageViewModel,**Mode=TemplatedParent}**}">
Run Code Online (Sandbox Code Playgroud)

我总是得到:

System.Windows.Data错误:4:无法找到带引用的绑定源.....

更新 我正在设置我的ViewModel吗?不认为这很重要:

 <Window.DataContext>
        <Binding Path="HomePage" Source="{StaticResource Locator}"/>
    </Window.DataContext>
Run Code Online (Sandbox Code Playgroud)

我使用MVVMLight工具包中的ViewModelLocator类来完成这项工作.

wpf binding mvvm icommand

20
推荐指数
4
解决办法
3万
查看次数

标签 统计

binding ×1

icommand ×1

mvvm ×1

wpf ×1