ValueTuple With WPF Binding [duplicate] - wpf

This question already has answers here:
Is it possible to bind to a ValueTuple field in WPF with C#7
(3 answers)
Closed 4 years ago.
why binding to ValueTuple property members (like Item1, Item2 ect) dont work?
<TextBlock x:Name="txtTest" Text="{Binding Item1}" />
the code:
txtTest.DataContext = ("Item A", "Another Item..");
output window:
BindingExpression path error: 'Item1' property not found on 'object' ''ValueTuple`2'
However in Tuple It always worked.

As stated in the documentation, Item1 and Item2 of a ValueTuple are fields rather than properties and you can only bind to public properties in WPF.
So if you want to be able to bind to a tuple, you should use the Tuple class.

Related

Bind selected text in TextBox to the view model in WPF and by MVVM [duplicate]

This question already has answers here:
MVVM and the TextBox's SelectedText property
(5 answers)
Closed 3 years ago.
I am new to WPF and MVVM.
I have a TextBox with some lines of text. I want to bind a piece of text highlighted by a user, to my ViewModel.
How can I do this without coding in code behind?
You cannot directly bind your selected text to a property. You have to implement a DependencyProperty and attach that but this is not trivial for beginners. A good explanation is here:
MVVM and the TextBox's SelectedText property

Which DataContext is used for my UserControl in the DataTemplate of a DataGrid? [duplicate]

This question already has answers here:
Issue with DependencyProperty binding
(3 answers)
How to correctly bind to a dependency property of a usercontrol in a MVVM framework
(4 answers)
Closed 5 years ago.
I have a datagrid, which is filled by a datatable. Each entry in the table is an object of a custom type. For each cell I want to use an usercontrol to show the content. I use XamlReader to create the usercontrol in code behind.
I learned that the DataContext of the DataTemplate for the columns CellTemplate is a DataGridRow and I could access an element with the following code:
$#"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<TextBox Text=""{{Binding [{count}].Amount}}"" />
</DataTemplate>"
When I try the same with my usercontrol it fails:
$#"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<local:MyControl
MyProperty=""{{Binding [{count}]}}""
xmlns:local=""...
with the error:
BindingExpression path error: '[]' property not found on 'object'
Is the DataContext different when I use custom control? I don't get it...

Use StringFormat for special output in XAML-Binding [duplicate]

This question already has answers here:
StringFormat is ignored
(3 answers)
Closed 6 years ago.
I have an integer value named Id and want a Label in Xaml to display the following:
(ID: 160)
I tried the following:
<Label Content="{Binding Id, StringFormat='(ID: {0:0})'} />
but it doesn't work - it just displays the value of Id:
160
How can I get this working without using a special ValueConverter class?
You have to use ContentStringFormat for content controls instead of the binding's StringFormat, has been asked various times before, will have to look for that.

Silverlight: Access element in DataForm EditTemplate by name [duplicate]

This question already has answers here:
How can I find WPF controls by name or type?
(21 answers)
Closed 2 years ago.
How does one access a control in a DataForm's EditTemplate from the code behind?
The following EditTemplate applies:
<toolkit:DataForm ItemsSource="{Binding ElementName=someDomainDataSource, Path=Data, Mode=TwoWay}">
<toolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
....
<sdk:DatePicker DisplayDate="{Binding DueDate, Mode=TwoWay}}"
x:Name="dpCustomMaterialDueDate"/>
....
</StackPanel>
</DataTemplate>
</toolkit:DataForm.EditTemplate>
</toolkit:DataForm>
Is it possible to access the DatePicker from the code-behind file using the variable name dpCustomMaterialDueDate? Intellisense seems unable to find it.
Also tried to access it in the DataForm's ContentLoaded event, but no luck, i.e.
dataformPrintOrders.ContentLoaded += (sender, args) =>
{
DatePicker d = (DatePicker)
dataformPrintOrders.FindNameInContent("dpCustomMaterialDuedate");
if (d != null)
{
d.DisplayDateStart = DateTime.Now.AddMonths(-1);
d.DisplayDateEnd = DateTime.Now.AddMonths(12);
}
};
The variable d is always null.
You can also attached a Loaded event handler, and cast the sender parameter to DatePicker

Why does WPF have x:Name and Name XAML? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In WPF, what are the differences between the x:Name and Name attributes?
Why does WPF have x:Name and Name XAML? What's the deal with x:Name?
Long story short: x:Name is an attached property and can thus be set on everything. Name is not an attached property and thus is only available on things that expose a Name property. WPF aliases Name to x:Name so you are safe to always use x:Name and this is what is recommended as you can use it everywhere.

Resources