WPF - Trouble binding ToolTip text on custom user control - c#

I'm in the process of creating a simple user control; just an ImageButton.
I've already successfully bound the Image to the button and so I've decided to add a tooltip. Now I'm having troubles. It seems that I can hard-code the text for the tooltip in the XAML for the control, but when it's bound it's returning an empty string.
Here's the XAML for my control:
<Button x:Class="BCOCB.DACMS.Controls.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Name="this"
Style="{StaticResource DisabledButton}">
<Image Source="{Binding ElementName=this, Path=Source}" />
<Button.ToolTip>
<TextBlock Text="{Binding ElementName=this, Path=ToolTipText}" />
</Button.ToolTip>
</Button>
And here's the dependency property info for the tooltip text:
public static readonly DependencyProperty ToolTipTextProperty = DependencyProperty.Register("ToolTipText", typeof(string), typeof(ImageButton));
public string ToolTipText
{
get
{
return this.GetValue(ToolTipTextProperty) as string;
}
set
{
this.SetValue(ToolTipTextProperty, value);
}
}
And, finally, the declaration of the control in my Window:
<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />
As I mentioned before, the image binds just fine and I've done it in exactly the same manner.
Any ideas?
Thanks,
Sonny
EDIT: I have it working now. I've removed the ElementName from the binding and set the TextBlock's DataContext = this in the code behind on instanciation. Still, I'd like to know how to fix this in the XAML, instead.

I'm unable to test this right now, but you can try:
<Button.ToolTip
DataContext=”{Binding Path=PlacementTarget.Parent.Parent,
RelativeSource={x:Static RelativeSource.Self}}"
>
<TextBlock Text="{Binding Path=ToolTipText}" />
</Button.ToolTip>
You may have to experiment a little with the number of "Parent" in PlacementTarget.
Hopefully this works. I don't like giving answers that I haven't tested, but I don't have VS on this computer. :)

I've had this same problem with binding to a ContextMenu. After my research I think that it is because the ToolTip and ContextMenu do not exist within the visual tree of your page/window/control. And therefore the DataContext is not inherited and makes binding troublesome.
Here is a Xaml hack I found that worked for me.
Binding to a MenuItem in a WPF Context Menu

The way to set the data context to "this" through xaml looks like this:
<Control DataContext={Binding RelativeSource={RelativeSource Self}}>
As another point, wpf buttons allow their content to be just about any (single) thing you want. If you want something other than text (ie, text and an image), it looks like this:
<Button Name="SampleButton" Click="SampleButton_Click">
<Grid Width="70" Height="62">
<Label Content="SampleText"/>
<Image Margin="3,3,3,3" Source="Graphics/sample.ico"/>
</Grid>
</Button>

Since you aren't changing anything but the Text on the tooltip TextBlock you can just use an inline declaration which will generate the TextBlock for you and doesn't require any hacking to get around the name scoping issue you're running into otherwise:
<Button ... ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=ToolTipText}">...
You could alternately set the ToolTip on the Image and use the control as the DataContext, which gets around the name scoping problem. The DataContext will be passed to the ToolTip, allowing normal binding:
<Image DataContext="{Binding ElementName=this}" Source="{Binding Source}">
<Image.ToolTip>
<TextBlock FontSize="18" Text="{Binding Path=ToolTipText}" />
</Image.ToolTip>
</Image>
This way allows additional settings on the TextBlock or more complex visuals.

This fixes the Problem with the Tooltip Bindings and Dependencies Properties:
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls"
x:Name="UserControl"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBox x:Name="textBox">
<TextBox.ToolTip>
<ToolTip Content="{Binding Path=CustomToolTip}" Background="Yellow"/>
</TextBox.ToolTip>
</TextBox>
</UserControl>
Instead of this ( doesnt Work ):
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls"
x:Name="UserControl">
<TextBox x:Name="textBox">
<TextBox.ToolTip>
<ToolTip Content="{Binding ElementName=UserControl, Path=CustomToolTip}" Background="Yellow"/>
</TextBox.ToolTip>
</TextBox>
</UserControl>

Related

Accidently instaniating an viewmodel twice in XAML? [duplicate]

<UserControl x:Class="WatermarkTextBox"
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"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="250">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<Border>
<Grid x:Name="grid">
<TextBlock Text="{Binding Watermark, FallbackValue=This prompt dissappears as you type...}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Name="txtUserEntry"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Border>
</UserControl>
The above code shows my WatermarkTextBox control. In the code behind file I have set the DataContext. I left out all the code for the DP's of the control.
public WatermarkTextBox()
{
InitializeComponent();
grid.DataContext = this;
}
I had to bind the DataContext to the grid because otherwise the Text properties of both the watermark and actual text wouldn't display. The problem now is that I can't set the Background of the Border outside of the Grid.
I tried the code below but then only the Background of the Border is set and not the watermark and actual text.
public WatermarkTextBox()
{
InitializeComponent();
this.DataContext = this;
grid.DataContext = this;
}
In a UserControl like this you should never exlicitly set the DataContext to this or anyting else, because the DataContext is usually set externally when you use the UserControl somewhere in your application. The externally applied DataContext is typically (part of) the application's view model.
You should instead change your internal bindings so that they use an explicit RelativeSource:
<TextBlock
Text="{Binding Path=Watermark,
RelativeSource={RelativeSource AncestorType=UserControl},
FallbackValue=This prompt dissappears as you type...}"
Visibility="{Binding ElementName=txtUserEntry,
Path=Text.IsEmpty,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox
Name="txtUserEntry"
Text="{Binding Path=Text,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
and then remove any DataContext assignment from the UserControl's constructor.
See e.g. this answer (and many other similar) that discuss this topic in detail.

C# WPF: Changing PlacementTarget of a ToolTip

I'm trying to change the PlacementTarget of a ToolTip to a window further up the visual tree in order to have custom ToolTip clipping effects in that window. I've hooked everything up except for the PlacementTarget. Here's an example from XAML and in code...neither work. This style is currently being used for a single tooltip attached to a TextBox.
<Style TargetType="ToolTip">
<Setter Property="ToolTipService.PlacementTarget"
Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Grid }} }" />
</Style>
If I go into code and look at the tooltip.PlacementTarget once it's attached to something...it's always set to the TextBox. I've tried multiple ways of using the VisualTree to get different UIElements. Nothing seems to work...so I'm assuming I'm not understanding or missing something.
The thing that really gets me is that if I go into my code and look at the PlacementTarget of the tooltip, it won't let me set it to anything else. For instance:
var ancestors = toolTip.PlacementTarget.GetSelfAndAncestors();
foreach(var ancestor in ancestors)
{
if(var ancestor is Grid)
{
// Conditional always hits.
// Before this line, PlacementTarget is a TextBox.
toolTip.PlacementTarget = (UIElement)ancestor;
// After the line, PlacementTarget is still a TextBox.
}
}
What am I doing incorrectly or not understanding?
Edit for Context: The custom clipping effect is to basically just find the closest ancestor window to the ToolTip's target and use that to make sure the ToolTip never goes outside the bounds of that window.
A short sample setting a Tooltip, using a property on the parent Window as PlacementTarget.
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Tag="Bar">
<Window.Resources>
<ToolTip x:Key="FooToolTip">
<StackPanel>
<TextBlock Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
</StackPanel>
</ToolTip>
</Window.Resources>
<Grid>
<TextBlock
Text="Foo"
ToolTip="{StaticResource FooToolTip}"
ToolTipService.PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Width="50">
</TextBlock>
</Grid>
</Window>
EDIT
To answer your questions,
the first snippet uses ToolTipService in the wrong way:
The ToolTipService class attached properties are used to determine the placement, behavior, and appearance of a tooltip. These properties are set on the element that defines the tooltip.
Applied in a style:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Tag="Bar">
<Window.Resources>
<ToolTip x:Key="FooToolTip">
<StackPanel>
<TextBlock Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
</StackPanel>
</ToolTip>
<Style x:Key="ToolTipStyle">
<Setter Property="ToolTipService.ToolTip" Value="{StaticResource FooToolTip}"/>
<Setter Property="ToolTipService.PlacementTarget" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</Style>
</Window.Resources>
<Grid>
<TextBlock
Text="Foo" Style="{StaticResource ToolTipStyle}"
HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Width="50">
</TextBlock>
</Grid>
</Window>
As for your second snippet in code behind, you can't set the PlacementTarget once the ToolTip is open and when the ToolTip is closed the PlacementTarget is null. As #mm8 pointed out, this has to do with the ToolTip and the PlacementTarget being in different visual trees, since a ToolTip spawns
a Window of its own.

Displaying Image in ComboBox's selected item

I have a custom template for my WPF windows ComboBoxes, which display items that inherit from ComboBoxItem that also have an Image property (so that my items can display both text and an image). Both the text and image display as expected in the ComboBox's popup menu for any given item, but I haven't been able to display an image in the currently selected item.
The ContentPresenter within the ComboBox template that displays the currently selected item has its Content property properly bound to {TemplateBinding SelectionBoxItem}, and its ContentTemplate is bound to the following static resource:
<DataTemplate x:Key="SelectionBoxItemTemplateTextAndImage" DataType="{x:Type SB:SBComboBoxItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="{Binding Image}"/>
<TextBlock Grid.Column="1" Text="{Binding}"/>
</Grid>
</DataTemplate>
SBComboBoxItem is the custom control that inherits from ComboBox and contains an "Image" property properly registered as a DependencyProperty.
I have attempted alternative implementations of that DataTemplate, including using:
{Binding Path=Image, RelativeSource={RelativeSource TemplatedParent}}
as the image's source. That didn't work, even though the text still displays when i set the TextBlock's Text property to:
{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}
I've played around and found numerous implementations that display the text, but the equivalents never work for the image. I don't understand why this doesn't work, as setting up the popup to display both images and text was a breeze.
Edit: Here's the added functionality to ComboBoxItem to handle the image, in case there's something I did wrong there:
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(Image), typeof(SBComboBoxItem));
public Image Image { get { return (Image)GetValue(ImageProperty); } set { SetValue(ImageProperty, value); } }
And here is where I have a ComboBox with the items added:
<ComboBox SelectedIndex="1">
<SB:SBComboBoxItem Content="Text">
<SB:SBComboBoxItem.Image>
<Image Source="..\Images\Table.png"/>
</SB:SBComboBoxItem.Image>
</SB:SBComboBoxItem>
<SB:SBComboBoxItem Content="MoreText">
<SB:SBComboBoxItem.Image>
<Image Source="..\Images\Euclidian.png"/>
</SB:SBComboBoxItem.Image>
</SB:SBComboBoxItem>
</ComboBox>
Despite the fact that I was using a DataTemplate to expose the underlying type of my selected combobox item through DataTemplate's DataType property, the SelectionBoxItem property of my ComboBox was apparently returning something that could not be cast as my custom ComboBoxItem. I don't know why this is, nor why it did not fire any runtime errors, but I found that by binding to ComboBox's SelectedItem property instead of SelectionBoxItem in the ContentPresenter allowed me to access user defined properties.
Does the DataTemplate even work? As far I remember, any WPF element that can be rendered, wont use a DataTemplate. And your custom ComboBoxItem can be rendered without the DataTemplate.
So the first question is, is the DataTemplate even applied? (Just change the background of DataTemplates' grid and find that out, if it works).
Let's assume the DataTemplate actually works, what I would try:
{Binding Path=Image, RelativeSource={RelativeSource AncestorType={x:Type SB:SBComboBoxItem}}
or RelativeSource as TemplatedParent with Content.Image
Okay, on the DataTemplate part, you have to bind against Image's Source property, not against Image itself.
Like so:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BitmapImage x:Key="validImg" UriSource="test.png" />
<DataTemplate x:Key="TEMP" DataType="ComboBoxItem">
<StackPanel Orientation="Horizontal">
<!--Not working Content(Image) is not valid for Source property I think-->
<Image Source="{Binding Path=Content}" />
<!--Working -->
<Image Source="{Binding Path=Content.Source}" />
<TextBlock Text="{Binding Path=Content}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter
Content="{Binding Items[0], ElementName=test}"
ContentTemplate="{StaticResource TEMP}">
</ContentPresenter>
<ComboBox x:Name="test" Height="50" Margin="0, 50, 0, 0">
<ComboBoxItem >
<ComboBoxItem.Content>
<Image Source="{StaticResource validImg}" />
</ComboBoxItem.Content>
</ComboBoxItem>
</ComboBox>
</Grid>

How to use ContentTemplate to avoid "Specified element is already the logical child of another element. Disconnect it first" error

I know this question has been asked countless number of times, but I don't understand half of what their problem is or how to follow their example.
The one I did find her name Rachel, posted a blog about it but her explanation was too brief..
http://rachel53461.wordpress.com/2011/07/17/wpf-error-specified-element-is-already-the-logical-child-of-another-element-disconnect-it-first/
Here was what I had before trying to follow here example:
<Window x:Class="Graph.View.MainView.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
Title="Main" Height="350" Width="525"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking">
<DockPanel LastChildFill="True">
<DockPanel>
<Label Content="{Binding ScreenContent}" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</DockPanel>
</DockPanel>
Because ScreenContent is having a logical parent I can't reuse it. After trying to follow Rachel's example:
<Window x:Class="Graph.View.MainView.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
Title="Main" Height="350" Width="525"
>
<Window.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<DockPanel>
<Label Content="{Binding ScreenContent}" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</DockPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DockPanel LastChildFill="True">
<Label Style="{StaticResource MyCustomContentControl}"/>
</DockPanel>
</Window>
How can I fix it? It isn't showing anything at all...
Thanks.
Please answer me what is ScreenContent property ... is it string? or some GUI element?
If it is a String then the follow the soltuion below ...
Also there are a few concepts that you must understand...
Label inside Label is a bad UI design.
ContentControls such as Label, Button etc. follow a particular XAML pattern when we want to set their ControlTemplate and DataTemplate.
In your case you want to assign a data context based property ScreenContent. So the DataTemplate way is correct.
However this will not flow to the Label inside your DataTemplate as the outer Label (to which you have applied the Style to) isnt set on its own Content property.
So basically a ContentControl.ContentTemplate works ONLY if a not null Content is set on that ContentControl and inside that DataTemplate (ContentTemplate) the Content works as the DataContext for all the items!
Notice the {Binding} expression below... It is self explanatory!!
<DockPanel LastChildFill="True">
<DockPanel.Resources>
<Style x:Key="MyCustomContentControl"
TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<DockPanel>
<ContentPresenter
Content="{Binding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</DockPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DockPanel.Resources>
<Label Style="{StaticResource MyCustomContentControl}"
Content="{Binding ScreenContent}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
</Label>
</DockPanel>
If it is a UserControl follow the solution below...
Are you assigning it only once (to the given Label)? or you are attempting to assign it as content of such various elements?
If its assigned ONLY once, then the solution above should work.
But if you are planing to assign it as a Content of multiple target elements then you need to follow the Model / ViewModel approach.
All your GUI elements should be hosted in XAML.
Code Behind should not deal with GUI elements.
Code behind works only on the soft copy of the GUI elements i.e. a data specific to fields that GUI (UserControl) represents.
E.g.
Example 1...
Suppose you have a TitleControl which is like a bold, italic WPF Label showing the text of a title. Your DataContext has such TitleControl object held which has some title text set to it.
Then in the ViewModel approach, you should create an instance of TitleControlViewModel as part of your DataContext... something like this...
YourDataContext.MyTitleControlViewModel = new TitleControlViewModel();
Inside the TitleControlViewModel class we should have a public property called MyTitle of type string.
YourDataContext.MyTitleControlViewModel.MyTitle = "My Title";
Now your template specific XAML should look like this...
<DataTemplate x:Key="MyTitleControlDataTemplate">
<local:TitleControl Title="{Binding MyTitle}"/>
</DataTemplate>
and your host panel should look like this...
<ContentControl ToolTip="Title is shown here..."
Content="{Binding MyTitleControlViewModel}"
Contenttemplate="{StatiocResource MyTitleControlDataTemplate}"/>
<ContentControl ToolTip="Same title control is shown here also !!!"
Content="{Binding MyTitleControlViewModel}"
Contenttemplate="{StatiocResource MyTitleControlDataTemplate}"/>
So this way same title control seems to be hosted on the tow content controls above. But if you think really, there are two different instances of TitleControl who are merely representing same MyTitleControlViewModel thus looking like a same control!
Google for how Data Templates, MVVM fit together.
In case you want to reuse ScreenContent (although its already a child of some other element) then you will have to first clone it and use the cloned control.
You can clone a control by first serializing it using XamlWriter and then create a new control by deserializing it using XamlReader, something like this -
string screenContentXml = XamlWriter.Save(ScreenContent );
//Load it into a new object:
StringReader stringReader = new StringReader(screenContentXml );
XmlReader xmlReader = XmlReader.Create(stringReader);
UIElement screenContentClone = (UIElement)XamlReader.Load(xmlReader);
use this screenContentClone in your window.
But, you may find yourself applying workarounds to make it work as there are some limitations in using XamlWriter.Save (like with bindings) - Serialization Limitations of XamlWriter.Save
Here are some other approaches for serialization -
An XAML Serializer Preserving Bindings
XamlWriter and Bindings Serialization
Although your design doesn't look correct as AngelWPF mentiond and you should try and re-design your window, and you might not require doing all this.
UserControls (or any other type of UI control) do not belong in the ViewModel
It would be far better to have an object that you are binding to, and to use a DataTemplate to tell WPF how to draw that object using your UserControl
For example, if you had
ScreenModel ScreenContent { get; set; }
where ScreenModel is a custom class that looked something like this
public class ScreenModel
{
public string Name { get; set; }
...
}
Then you would bind to it using something like this:
<DockPanel LastChildFill="True">
<DockPanel>
<ContentControl Content="{Binding ScreenContent}" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</DockPanel>
</DockPanel>
And use a DataTemplate to tell WPF how to draw the ScreenModel class
<DataTemplate DataType="{x:Type models:ScreenModel}">
<views:ScreenContentUserControl />
</DataTemplate>
or
<DataTemplate DataType="{x:Type models:ScreenModel}">
<Label Content="{Binding Name}" />
</DataTemplate>
Because you are using a Template, WPF will create a new copy of the UserControl whenever its needed, and won't try to use the same UserControl in multiple spots.
Your error is occurring because you are adding the same UserControl (ScreenContent) to the VisualTree more than once through the Content="{Binding ScreenContent}" binding.
Even doing something like switching tabs will probably cause this error because you will be unloading all the UI objects by switching away from the tab, then loading new UI objects by switching back, however your ScreenContent user control already has it's parent set to an old object that no longer exists.

Escape from DataContext

I have a window that get its data from another class that is passed as DataContext. But I now also want to do data binding within the window. The window looks as follows:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300">
<StackPanel>
<TextBlock Text="{Binding UserName}" />
<TextBlock x:Name="TestTextBlock"
Text="Hello World" />
<TextBlock x:Name="TestTextBlock2"
Text="{Binding ElementName=TestTextBlock,Path=Text}" />
</StackPanel>
</Window>
The binding between the text blocks TestTextBlock and TestTextBlock2 works fine, but only until I change the DataContext-property of the window. How can I bind between those two textblocks so that changing the DataContext will not break the data binding?
Thanks in advance,
Stefan
Try setting the Binding.Mode to OneTime explicitly.
That way, TestTextBlock2.Text will only be set once (if that's what you want).

Categories

Resources