WFP数据绑定依赖项
本文关键字:依赖 数据绑定 WFP | 更新日期: 2023-09-27 18:21:10
在发布XAML之前进行总结,此表单目前非常简单。ListBox有一个ListBox、两个按钮和一个上下文菜单。
如果在ListBox中单击鼠标右键,并且选择了NO ELEMENT,则我的上下文菜单将执行ADD操作,现在只是弹出一个消息框。
当选择一个元素(如MODIFY)时,我的绑定将不起作用。因此,经过大量阅读,我假设我在某个地方有继承问题。我试过使用DataContext、RelativeSource等,但仍然没有什么乐趣。
这是我的XAML
<Window x:Class="FracasReportSettings.MainWindow"
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:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
Height="402"
Width="578"
Title="FRACAS Ticket Value Modification"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="91*"/>
<ColumnDefinition Width="194*"/>
</Grid.ColumnDefinitions>
<TextBlock FontSize="36"
FontWeight="Bold"
Foreground="Purple"
Text="{Binding PageTitle}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextWrapping="Wrap" Margin="266,27,10,9" Width="112" Grid.Column="1" />
<StackPanel HorizontalAlignment="Left" Height="311" Margin="40,22,0,0" VerticalAlignment="Top" Width="111">
<Button Content="Detection Method" Margin="10,10,10,10"
Command="{Binding MyBinding}"
CommandParameter="DetectionMethod"/>
<Button Content="Button" Margin="10,0,10,0"/>
</StackPanel>
<ListBox
Name="AdminList"
ItemsSource="{Binding Names}"
Height="302" Width="231" Margin="151,22,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="2"
SelectedItem="{Binding SomeName}"
>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
**<-- THIS WORKS FINE -->
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Add"
Command="{Binding RCContentMenu}">
</MenuItem>
</ContextMenu>
</ListBox.ContextMenu>**
<--MY ERROR IS IN HERE-->
**<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext}">
<MenuItem Header="Modify"
Command="{Binding Path=ModifyElementCommand}"
CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>**
</ListBox>
</Grid>
ModifyElementCommand位于名为MainViewModel的ViewModel中。
我从输出窗口收到的错误是:
System.Windows.Data错误:4:找不到与绑定的源引用"RelativeSource FindAncestor,AncestorType='System.Windows.Controls.ListBox',AncestorLevel='1'。BindingExpression:Path=DataContext;DataItem=null;目标元素是"ContextMenu"(名称=");目标属性为"DataContext"(类型"对象")
我知道这意味着无法找到依赖项。
我该怎么办才能解决这个问题?
下面是XAML:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag.DataContext}">
<MenuItem Header="Modify"
Command="{Binding Path=ModifyElementCommand}"
CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Tag" Value="{Binding ElementName=AdminList}" />
</Style>
</ListBox.ItemContainerStyle>