Binding a TextBlock 's Text property to current existing DataContext - c#

For a TextBlock control, I want the Text property to be depending on the current DataContext, and I will display some very nice formatted string-- depending on the type of the DataContext-- with an IValueConverter to properly convert the Text property.
How to do this?

A Binding without a Path (or with Path=.) binds directly to the object in the DataContext of the the TextBlock:
<TextBlock Text="{Binding Converter={StaticResource ...}}" />

Related

How to show tooltip for IntegerUpdown control?

I try to make tooltip on DevExpress IntegerUpDown (DoubleUpDown and so on) control:
<xcd:DoubleUpDown
HorizontalAlignment="Stretch"
Margin="5,2"
Grid.Column="0"
Minimum="0"
Value="{Binding SomeValue,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
Text="{Binding SomeValue,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
ToolTip="{Binding SomeValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
>
</xcd:DoubleUpDown>
But, it do not work for me - only empty tooltip shows.
How to make tooltip?
If you see an empty ToolTip at runtime, it is your binding that fails.
Try to set the ToolTip property to a hard-coded value and you should see that it works:
ToolTip="Test"
You then know that there is something wrong with your binding. Make sure that the DoubleUpDown control has a DataContext that has a public SomeValue property that returns the value you are expecting to show up in the tooltip.
Also note that it makes no sense to set the Mode property to TwoWay and the UpdateSourceTrigger property to PropertyChanged on a ToolTip binding.

How to apply a Binding property to a WPF binding?

I have a WPF UserControl. In the cs part, I have a property of type System.Windows.Data.Binding called ElementBinding.
Here is what the xaml file looks like:
<ItemsControl ItemsSource="{Binding ElementName=ControlRoot, Path=FilteredSource}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ElementBinding, ElementName=ControlRoot}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
This doesn't work as intended as it displays (quite logically) the ToString() method of the Binding property.
What I would like to do is appy the Binding property of my UserControl to the Text property of my TextBlock, using the TextBlock DataContext.
I know I can always change my Binding property to a string property and use a converter and reflection to access the path, but I would prefer with a Binding property if it is possible. I guess it is since it's used, for example, in a DataGridColumn.

How To Get Text From Binded TextBlock Store App c#

I want to get text from below TextBlock but i can't get it ??
Note : fname is property for setting text in TextBlock
<TextBlock Name="getname" Text="{Binding Path=fname }"/>
By default, the text binding for a TextBlock is One-Way. You'll need to explicitly specify a Two-Way binding if you want to read the value back into your view model.
Text="{Binding Path=fname, Mode=TwoWay}"
If you're not using view models and you just want programmatic access to the text value you'll need to access the value in code-behind.
public string GetTextBlockText()
{
return this.getname.Text;
}

How to bind to property of object returned by converter

I'm trying to bind to a value, run a converter over it, and then display a property of that value. Having the Converter directly return the property I want wouldn't work, as I need property changes to be tracked.
What I'm trying to achieve would be something like this:
// NOTE: FOLLOWING IS NOT SUPPORTED BY WPF
// A 'Binding' cannot be set on the 'Source' property of type 'Binding'.
// A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Text={Binding TextField Source={Binding SomeValue, Converter={StaticResource GetObjectFromValueConverter}}}`
Ideally, this would all be wrapped up in a simple markup extension.
Text={l:GetTextField SomeValue}
Problem is, I haven't been able to find any way to do this other than bind the Tag of the element to the converter, and then bind the target field to the property as follows:
Tag={Binding SomeValue, Converter={StaticResource GetObjectFromValueConverter}}
Text={Binding Tag.TextField, RelativeSource={RelativeSource Self}}
This is obviously cumbersome, limited (you only get one Tag field) and feels abusive. How else can I go about achieving what I want whilst monitoring for changes of TextField though?
You can bind the DataContext of the TextBox, instead of the Tag. That will make your other bindings much simpler:
DataContext="{Binding SomeValue, Converter={StaticResource GetObjectFromValueConverter}}"
Text="{Binding TextField}"
This assumes that you do not have any other bindings on the TextBox that require the inherited DataContext. For example, in the bindings below Text2 would be broken:
DataContext="{Binding SomeValue, Converter={StaticResource GetObjectFromValueConverter}}"
Text2="{Binding SomeOtherValue, Converter={StaticResource GetObjectFromValueConverter}}"
Text="{Binding TextField}"
Also, if you have a more complex control other than TextBox, the DataContext for any controls below it in the logical/visual tree will also be affected.

In WPF how to get binding of a specific item from the code?

The example of this would be:
A textBox is bound to some data. There is a second text box which is not bind to anything. So I want to bind text box 2 to the same data 1st textBox is bound.
In other words I wan't to know if the DependencyObject stores some reference to it's data-bindings? If not, what is the way to find out all data-bindings of a specific object?
Try this
Xaml
<TextBox Name="textBox1" Text="{Binding Text1}"/>
<TextBox Name="textBox2" Text="No Binding"/>
Then we can set the binding of the TextProperty for textBox2 to the same as textBox1 with this code behind
BindingExpression bindingExpression = textBox1.GetBindingExpression(TextBox.TextProperty);
Binding parentBinding = bindingExpression.ParentBinding;
textBox2.SetBinding(TextBox.TextProperty, parentBinding);
You can get the binding of any dependency object using
System.Windows.Data.BindingOperations.GetBinding(DependencyObject target,DependencyProperty dp)
then set the binding with
System.Windows.FrameworkElement.SetBinding(DependencyProperty dp, string path)
For example:
var binding = BindingOperations.GetBinding(textBox1,TextBox.TextProperty);
textBox2.SetBinding(TextBox.TextProperty, binding);
I know there's already an accepted answer, but is there some reason you're just not doing this?
<TextBox Name="textBox1" Text="{Binding Text1}"/>
<TextBox Name="textBox2" Text="{Binding Text, ElementName=textBox1}"/>
Now whatever textBox1 is bound to, even if that binding changes, textBox2 is as well, no code-behind needed.
Granted I'm basing this on the XAML as presented, and you very well may need the binding itself from code for something else, but if not, the above works just fine.
You can do this in code by calling the SetBinding method.

Categories