Binding textbox text to images canvas.leftProperty - wpf

How can I bind the text of a textbox to the value of an image's Canvas.GetLeft() Property in code and XAML?
textBox1
canvas1
image1
<TextBox Name="textBox1" Width="100" Text="{Binding ElementName=image1, Path=Canvas.LeftProperty}"></TextBox>
doesn't work.

It's an attached property, you need parentheses, also you use the proper DP name, not the field:
Path=(Canvas.Left)
(Read up on syntax details like this on MSDN)

Related

Data Binding Path with TextBox WPF [duplicate]

How can I bind the text of a textbox to the value of an image's Canvas.GetLeft() Property in code and XAML?
textBox1
canvas1
image1
<TextBox Name="textBox1" Width="100" Text="{Binding ElementName=image1, Path=Canvas.LeftProperty}"></TextBox>
doesn't work.
It's an attached property, you need parentheses, also you use the proper DP name, not the field:
Path=(Canvas.Left)
(Read up on syntax details like this on MSDN)

Binding properties of selected object in combobox to TextBox in WPF

I'm using MVVM to load text files and to show their content.
Model
MyFile.cs has a Name and Text // Implements the INotifyPropertyChanged
MyFileRepository.cs // collection of my loaded files
ViewModel
OpenFileCommand to load a file and add it to the _filerepository object
FileCollection that's bound to the View
View
Button to fire the OpenCommand
ComboBox to show the names of the loaded files
TextBox to show the content of selected file in combobx
<Button Name="OpenFile" Command="{Binding OpenFileCommand}">
<ComboBox Name="FilesList" ItemsSource="{Binding Path=FileCollection}" DisplayMemberPath="Name" />
<TextBox Name="FileContent" Text="{Binding the Text of selected file in combobx "/>
How to bind the Text property of MyFile selected in combobx to the TextBox?
The easiest approach would be element binding:
<TextBox Name="FileContent"
Text="{Binding SelectedItem.Text,ElementName=FilesList} />
So that's binding to the Text property of the SelectedItem in your FilesList ComboBox, which (if everything's wired up the way I think it is) is of type MyFile.
Without element binding you could add the property "SelectedItem" (Type: MyFile) to your VM and bind it to the SelectedItem property of your combobox (mode=twoway).
Now your TextBox.Text-Property should look like:
<TextBox Name="FileContent"
Text="{Binding SelectedItem.Text} />

Bind a ComboBox to two DataContexts

I have a ComboBox in my wpf application.
It's ItemsSource is binded to some table in my DataSet.
I need the text property to be binded to another's object property . I doesn't work because the ComboBox doesn't want to get two DataContexts. How can I solve this problem?
<StackPanel Width="Auto" Height="Auto" MinWidth="296" Orientation="Vertical" x:Name="MyStackPanel">
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding}" Text={Binding Path=MyProperty} />
</StackPanel>
In the code behind :
MyComboBox.DataContext = MyDataSet.Tables[MyTable];
MyStackPanel.DataContext = MyObject;
I want the ComboBox to show items from one DataContext but to show the text from another DataContext. How can I do it?
Don't use DataContext. Set the Source property of your bindings in XAML or create the bindings in code and set the Source property there.
Why are you assigning something to the datacontext of the stackpanel? From the looks of it, its not used.
Your code should work if MyDataSet.Tables[MyTable] returns an enumeration and contains a property called MyProperty.
What do you mean when you say that the combobox "doesn't want to get two DataContexts"?
Look into the properties IsEditable and IsReadOnly of the combobox.
Something like
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding}" Text={Binding ElementName=MyStackPanel Path=DataContext.MyProperty} />

WPF ListBox ListBoxItem Binding

I am going through the Sams book "Teach Yourself WPF in 24 Hours". At one point the authors show how you can bind a ListBox's selected-item value to a property. I get that, it's pretty straightforward. But when I try to create my own ListBox control with my own ListBoxItems, I can't seem to get it to work.
The ListBox that works uses a system collection as its ItemsSource property:
<ListBox x:Name="FontList"
DockPanel.Dock="Left"
ItemsSource="{x:Static Fonts.SystemFontFamilies}"
Width="160" />
The value selected from this ListBox is then used in a TextBlock as follows:
<TextBlock Text="Test"
FontFamily="{Binding ElementName=FontList, Path=SelectedItem}"
TextWrapping="Wrap"
Margin="0 0 0 4" />
Notice that the Path is set to SelectedItem.
Now, I wanted to set the FontSize using another ListBox that contains 3 different sizes. Here is what I did:
<ListBox x:Name="Size" >
<ListBoxItem>10</ListBoxItem>
<ListBoxItem>15</ListBoxItem>
<ListBoxItem>20</ListBoxItem>
</ListBox>
And then I added a binding to the Size attribute of the TextBox as follows:
<TextBlock Text="Test"
FontFamily="{Binding ElementName=FontList, Path=SelectedItem}"
Size="{Binding ElementName=Size, Path=SelectedItem}"
TextWrapping="Wrap"
Margin="0 0 0 4" />
The Size doesn't change when I run the program. So I tried to add the binding I was using for Size to the Text attribute--in order to see its value:
<TextBlock Text="{Binding ElementName=Size, Path=SelectedItem}""
FontFamily="{Binding ElementName=FontList, Path=SelectedItem}"
Size="{Binding ElementName=Size, Path=SelectedItem}"
TextWrapping="Wrap"
Margin="0 0 0 4" />
I see that it is changing as I click the Size ListBox, but I also see that the SelectedItem is displaying as this (when I click the 15 entry):
System.Windows.Controls.ListBoxItem:15
My questions:
1) What is the actual value being returned by the Path called SelectedItem? Is it "System.Windows.Controls.ListBoxItem:15" or is it "15"? If it's not 15, how can I specify a Path that returns just 15 and not System.Windows.Controls.ListBoxItem:15?
2)Why does the FontFamily SelectItem work? I realize that the FontList is coming from a System collection of font names, but it is unclear to me why the ListBox isn't returning a collection of ListBoxItems as text. If my ListBox's Path reference is returning a SelectedItem object of type ListBoxItem, then I would think I could use a Path of SelectedItem.Value or something like that--but it doesn't work and there is no Intellisense to help me.
I want to get THIS example working because it will help clear-up some misunderstandings I have. Please don't refactor the solution to get it to work some other way unless it's entirely impossible for me to have a Path reference that will give me just the numeric portion of my Size ListBoxItem that is selected.
What is the actual value being returned by the Path called SelectedItem?
It is System.Windows.Controls.ListBoxItem:15 (you can read this as "ListBoxItem with content set to 15"), that's why your binding does not work - it expects a numeric value, not ListBoxItem. You can specify Path as SelectedItem.Content to make this work. Also you can set SelectedValuePath of ListBox "Size" to "Content", and bind to SelectedValue property instead of SelectedItem.
Solution 1:
<TextBlock Size="{Binding ElementName=Size, Path=SelectedItem.Content}" />
Solution 2:
<ListBox x:Name="Size" SelectedValuePath="Content" />
<TextBlock Size="{Binding ElementName=Size, Path=SelectedValue}" />
Why does the FontFamily SelectItem work?
Because that ListBox contains a font collection, not a collection of ListBoxItems (they are still created to represent each item in a collection though). You can achieve the same behavior with font sizes if you define collection of font sizes in code and bind ListBox'es ItemsSource property to that collection or define contents of your ListBox as a collection of System.Double values directly in XAML:
<ListBox x:Name="Size"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:Double>10</system:Double>
<system:Double>15</system:Double>
<system:Double>20</system:Double>
</ListBox>
1) The actual value being returned by your SelectedItem binding is a ListBoxItem object. To get the value (15) from your binding you could use a converter or make your binding path a bit more explicit to get the listbox item's Content property value:
Size="{Binding ElementName=Size, Path=SelectedItem.Content}"
2) This is a covariant operation so the type of each list item is inferred from its source. The items generated by your font family items control (ListBox) are a result of the collection it's bound to. The Items property (populated via the ItemsSource dependency property) is an ItemCollection of generic objects which take on the type of their corresponding contextual objects.

Bind value between control in different view -- WPF / MVVM

in my MVVM application (in wpf)
i have two view and I want to bind the context of my label on my textbox value (in the other view)
SelectorView.xaml contain this control:
<TextBox x:Name="tbArt" value="XX"/>
DescriptionView.xaml contain this control:
<label context="{binding on the tbArt value????}">
Is that possible directly without going in code behing and viewmodels ?
Will the label be refresh automatically ?
Thanks !
If both of the controls databinds to the same property the label will refresh when the value is changed.Make sure that the property triggers property changed when it gets changed.
ex:
in XAML.
<TextBox x:Name="tbArt" value="{Binding Path=TheProperty, UpdateSourceTrigger=PropertyChanged}"/>
<label context="{binding TheProperty}">
in the textbox make sure you use:
UpdateSourceTrigger=PropertyChanged
. otherwise the property won't be changed until focus is shifted from the textbox.
If all you want to do is display the value of one control in another control within the same window/page, you could do the following:
<TextBox x:Name="tbArt" Text="XX" />
<Label Content="{Binding Path=Text", ElementName=tbArt}" />
This will bind the content of the Text of the label to a control with the name "tbArt". You can do the same thing with other properties of the control as well.
For instance,
<TextBox x:Name="tbArt" Text="XX" Width=33 />
<Label Content="{Binding Path=Width, ElementName=tbArt}" />
will display "XX" in the textbox and "33" in the label.

Resources