你遇到过这样的问题:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'ContextMenu' (Name=''); target property is 'DataContext' (type 'Object')
Run Code Online (Sandbox Code Playgroud)
码:
<ContextMenu DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
Run Code Online (Sandbox Code Playgroud)
上下文菜单位于:
ListBox-> ListBoxItemTemplate -> DataGrid -> DataGrid.ContextMenu
Run Code Online (Sandbox Code Playgroud)
我在ListBox控件中还有另一个Binding,它完全没有任何问题.
我已经创建了一个wpf应用程序,我在其中使用了MahApps Metro工具作为我的视图窗口.我的应用程序运行正常,但输出窗口中显示绑定错误.我没有使用该错误中提到的任何代码.
错误是:
无法找到与引用'RelativeSource FindAncestor绑定的源,AncestorType ='MahApps.Metro.Controls.Glow',AncestorLevel ='1''.BindingExpression:路径= GlowColor; 的DataItem = NULL; target元素是'SolidColorBrush'(HashCode = 9047482); 目标属性是'颜色'(类型'颜色')
xaml代码:
<Controls:MetroWindow
x:Name="MainWin"
x:Class="TimeSheet.DayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:TimeSheet.Views.DataTemplateSpace"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="DayView" Width="596" Height="596">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个基于WPF的Treeview
项目 -
子项目
如果选择了Subitem,我还想显示Item of Properties.
<StackPanel Grid.Column="2" DataContext="{Binding ElementName=myTreeView, Path=SelectedItem}">
<TextBox Text="{Binding Path=Name, Mode=TwoWay}" />
<TextBox Text="{Binding RelativeSource={???} Path=Name, Mode=TwoWay}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我想我需要使用RelativeSource语句,但不太确定如何这样做.
我在 TreeView 中有一个上下文菜单
UserControl (DataContext=ViewModel)
|
|
---- TreeView (ItemSource=MyItems)
|
|
----- Items (ItemSource=MyChildrenItems)
|
|
----- ContextMenu
Run Code Online (Sandbox Code Playgroud)
我想将 ContextMenuItem 的命令绑定到 ViewModel 中的 RelayCommand,我尝试了各种relativesource 绑定,但似乎没有任何效果...
我应该如何配置RelativeSource 绑定?
<ContextMenu>
<MenuItem
Header="Bla"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}, Path=DataContext.MyRelayCommand}" />
Run Code Online (Sandbox Code Playgroud)
我收到类似的绑定错误
无法找到引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.TreeView',AncestorLevel='1”的绑定源。BindingExpression:Path=DataContext.ExcludeSeasonCommand; 数据项=空;目标元素是“MenuItem”(名称=“”);目标属性是“Command”(类型“ICommand”)
我ControlTemplate为我的自定义控件创建了一个MyControl.
MyControl派生自System.Windows.Controls.Control并定义以下属性public ObservableCollection<MyControl> Children{ get; protected set; }.
要显示嵌套的子控件,我使用的是一个被a包围的ItemsControl(StackPanel)GroupBox.如果没有子控件,我想隐藏GroupBox.
应用程序启动时一切正常:如果Children属性最初包含至少一个元素,则显示组框和子控件.在另一种情况下,它是隐藏的.
当用户将子控件添加到空集合时,问题就开始了.这种GroupBox可见性仍然崩溃了.从集合中删除最后一个子控件时会出现同样的问题.在GroupBox仍然可见.另一个症状是HideEmptyEnumerationConverter转换器没有被调用.向非空集合添加/删除子控件按预期工作.
以下绑定有什么问题?显然它可以工作一次,但不会更新,虽然我绑定的集合是类型ObservableCollection.
<!-- Converter for hiding empty enumerations -->
<Common:HideEmptyEnumerationConverter x:Key="hideEmptyEnumerationConverter"/>
<!--- ... --->
<ControlTemplate TargetType="{x:Type MyControl}">
<!-- ... other stuff that works ... -->
<!-- Child components -->
<GroupBox Header="Children"
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Children, Converter={StaticResource hideEmptyEnumerationConverter}}">
<ItemsControl ItemsSource="{TemplateBinding Children}"/>
</GroupBox>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
.
[ValueConversion(typeof (IEnumerable), …Run Code Online (Sandbox Code Playgroud) wpf binding relativesource observablecollection controltemplate
在这里做一点测试.只有这两个文本框中的第一个显示值"123".为什么不是第二个?
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525">
<StackPanel Width="123" x:Name="Panel1">
<TextBox Text="{Binding ElementName=Panel1, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox>
<TextBox Text="{Binding Source={RelativeSource AncestorType={x:Type StackPanel}}, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我创建了一个自定义控件,MyTextBox从TextBox. 它有一个与之关联的样式,其中包含一个命名控件:
<Style x:Key="{x:Type MyTextBox}" TargetType="{x:Type MyTextBox}">
<!-- ... -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyTextBox}">
<!-- ... -->
<SomeControl x:Name="PART_SomeControl" />
<!-- ... -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
MyTextBox有一个依赖属性,当设置时,将其值传播到SomeControl:
public class MyTextBox : TextBox
{
// ...
public static new readonly DependencyProperty MyParameterProperty =
DependencyProperty.Register(
"MyParameter",
typeof(object),
typeof(MyTextBox),
new PropertyMetadata(default(object), MyParameterChanged));
private static void MyParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var me = (MyTextBox)d;
var someControl = (SomeControl)me.GetTemplateChild("PART_SomeControl");
someControl.SetValue(SomeControl.MyParameterProperty, e.NewValue);
}
}
Run Code Online (Sandbox Code Playgroud)
这在进行简单绑定时工作正常,如下所示: …
我有一组类似于以下内容的视图/视图模型:
CustomDialogView
CustomView
CustomListView
CustomControl
-SomeCustomProperty
Run Code Online (Sandbox Code Playgroud)
这些视图中的每一个都绑定到适当的视图模型。
我正在尝试将 SomeCustomProperty 绑定到 CustomDialogView 视图模型上的属性。
做这个的最好方式是什么?我尝试了一些事情,其中最有希望的似乎是通过 RelativeSource FindAncestor 设置此属性的绑定,例如:
<CustomControl
SomeCustomProperty="{
Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type sourcePath:CustomDialogViewModel}},
Path=SomeCustomProperty,
Mode=OneWay/>
</CustomControl>
Run Code Online (Sandbox Code Playgroud)
但我在这里根本没有任何约束力。
我不确定它是否有任何意义,但 CustomListView 是由工厂填充的。
wpf ×8
c# ×6
binding ×4
xaml ×2
contextmenu ×1
data-binding ×1
findancestor ×1
listbox ×1
mvvm ×1
treeview ×1