relativesource

WPF pass parent binding object to the converter

左心房为你撑大大i 提交于 2019-12-09 14:04:00
问题 I have ItemsControl that is bound to collection of type Student. Inside the ItemTemplate I have a TextBox that uses IValueConverter to do some custom calculations and logic. I want to pass the actual Student object to the value converter, instead a property of it. How can I do that? Here's a sample of my code. <ItemsControl ItemsSource="{Binding StudentList}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding ????, Converter=

ElementName vs. RelativeResource?

我与影子孤独终老i 提交于 2019-12-01 03:05:38
What of the following TextBlocks' Bindings costs more performance: <Window x:Name="Me" x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:WpfApplication1" Title="MainWindow"> <StackPanel> <TextBlock Text="{Binding Title, ElementName=Me}"/> <TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type src:MainWindow}}}"/> </StackPanel> </Window> I am sure my question might take different when the TextBlocks are in a high nesting level having many siblings

ElementName vs. RelativeResource?

a 夏天 提交于 2019-11-30 22:11:08
问题 What of the following TextBlocks' Bindings costs more performance: <Window x:Name="Me" x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:WpfApplication1" Title="MainWindow"> <StackPanel> <TextBlock Text="{Binding Title, ElementName=Me}"/> <TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type src:MainWindow}}}"/> </StackPanel> </Window> I am sure

Binding to DataContext outside current ItemsSource context

老子叫甜甜 提交于 2019-11-30 19:18:33
I have a DataSet bound to the Window.DataContext ; I also have a DataGrid : <DataGrid ItemsSource={Binding Tables[Items]}> <DataGrid.Columns> <DataGridTextBoxColumn Header={Binding Path=DataContext.Tables[Names]/Test, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}} /> </DataGrid.Columns> </DataGrid> Basically, I'm trying to bind the Header of that column to DataTable "Names", Column "Test", first row. However, I can't get it right. Note that I can bind it fine outside the DataGrid. The Grid's ItemsSource changes the data context and I don't know how to refer outside

What exactly does WPF Data Binding's “RelativeSource FindAncestor” do?

情到浓时终转凉″ 提交于 2019-11-30 04:44:24
I am currently working within a WPF user control (the root element of my XAML file is "UserControl"), which I know is being hosted inside a Window. How can I access a property of the Window using data binding? Does anyone know why simply <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" Path="..." /> does not work? The error message I get is: System.Windows.Data Warning: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. Edit: I ended up using a variation on ArsenMkrt's

What exactly does WPF Data Binding's “RelativeSource FindAncestor” do?

℡╲_俬逩灬. 提交于 2019-11-29 02:38:46
问题 I am currently working within a WPF user control (the root element of my XAML file is "UserControl"), which I know is being hosted inside a Window. How can I access a property of the Window using data binding? Does anyone know why simply <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" Path="..." /> does not work? The error message I get is: System.Windows.Data Warning: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor,

How to access a control from a ContextMenu menuitem via the visual tree?

て烟熏妆下的殇ゞ 提交于 2019-11-27 15:39:51
This seems to be a pretty popular topic, but... I have the following XAML: <internal:MyCommandObject x:Name="CommandModel"/> <Button DockPanel.Dock="Bottom" Command="{Binding DoAction, ElementName=CommandModel}"> <Button.ContextMenu> <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}"> <MenuItem Command="{Binding DoAction, ElementName=CommandModel}"/> </ContextMenu> </Button.ContextMenu> Click Me </Button> Now, MyCommandObject is a control which exposes dynamic commands from its DataContext . You know what's coming next. :) Basically, the button command

WPF Bind to parent property from within nested element using style

前提是你 提交于 2019-11-27 03:14:02
问题 I've been trying to build a text box with a hint that's displaying while it's empty. I'm having trouble setting the hint text from within a style. To be precise, this works (that is, it binds correctly): <TextBox Tag="hint text"> <TextBox.Background> <VisualBrush Stretch="None"> <VisualBrush.Visual> <TextBlock Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" FontStyle="Italic" Foreground="LightGray" /> </VisualBrush.Visual> </VisualBrush> </TextBox.Background> <

How to access a control from a ContextMenu menuitem via the visual tree?

不想你离开。 提交于 2019-11-26 17:18:30
问题 This seems to be a pretty popular topic, but... I have the following XAML: <internal:MyCommandObject x:Name="CommandModel"/> <Button DockPanel.Dock="Bottom" Command="{Binding DoAction, ElementName=CommandModel}"> <Button.ContextMenu> <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}"> <MenuItem Command="{Binding DoAction, ElementName=CommandModel}"/> </ContextMenu> </Button.ContextMenu> Click Me </Button> Now, MyCommandObject is a control which exposes

RelativeSource binding from a ToolTip or ContextMenu

那年仲夏 提交于 2019-11-26 11:49:46
What am I doing wrong here?: <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <Button> <Button.ToolTip> <TextBlock Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}" /> That's just a simplified example, that doesn't work anyway :) Actually I need to get a value from another property that is in scope of the Window's DataContext. Help me pls. HCL This is tricky because ToolTip is not part of the VisualTree. Here you see a cool solution for the same problem with ContextMenus. The same way you can go for the ToolTip. UPDATE Sadly the link is gone and I