Hi I have read some tutorials on Data Binding but I can't seem to find what I need which is a DataBinding of a List to a Multiple TextBox.
I'm not sure if this is possible on WPF and I'm quite new to Data Binding so bear with me.
I have a List(Global Variable)
and let say it has 3 words which are "Apple", "Banana" and "Orange"
this 3 words are given by the Code Behind of my XAML.
In my XAML I have 3 TextBox, from first to third I want to assign my List to it so from the
first textbox -> Apple
second textbox -> Banana
third textbox -> Orange
Now the catch here is that I need it to work in 2 ways which means for example
I edit the first textbox to "Mango", the List on the Code Behind will also change.
Is this possible on Data Binding?
You can just bind to the element of the list.
<TextBox Text="{Binding Path=FruitList[0]}" />
<TextBox Text="{Binding Path=FruitList[1]}" />
<TextBox Text="{Binding Path=FruitList[2]}" />
Edit:
The default binding method of a TextBox is TwoWay, but you can add this if you want to make it clear.
<TextBox Text="{Binding Path=FruitList[0]} Mode=TwoWay" />
You can also add UpdateSourceTrigger=PropertyChanged to make the List update on every key stroke.
Edit2:
To acces the objects gcreated in code behind you have to set tht DataContext of your WPF. See wpf xaml binding to object created in code behind
Related
I am trying to implement a list of programmatically instanced group of controls such as this one:
Example of my group
.
<Grid>
<TextBlock x:Name="TextBox_Data"/>
<TextBlock x:Name="TextBox_Time"/>
<TextBox x:Name="TextBlock_ID"/>
<ComboBox x:Name="ComboBox_Type"/>
<Button x:Name="Button_Data"/>
<Grid/>
It contains 2 TextBox, 1 TextBlock and 1 ComboBox and 1 button ( detail is pretty irrelevant tough) inside a Grid.
I would like to duplicate the Grid Parent to fill a list, and access to every values of the duplicated controls but I can not figure out how to do this.
I had in mind something equivalent to Android Studio java/xml combo but I couldn't find anything on this topic around here.
Any lead is more than welcome.
Thank you in advance for your time :)
Create a separate UserControl that contains your group of controls.
Do something similar to this:
WPF creating grid from XAML in code-behind
But, instead of code-behind, you can reference your new UserControl in a XAML.
I am fairly new to WPF and have two questions?
Question #1:
From my XAML snip below, by button "btnRed" word's fine with my code behind. When the button is clicked, the proper view is display. However, how does one perform the same thing "programmatically"? Hence, my next question.
Question #2:
I am not sure how to make a "textbox" and "button" work together to perform the same action. What I'm trying to do is this. (1) I would like the textbox to be linked to the "DataContext" of the button, "btnDisplayView". (2) so when I type in, say, "RedView" into the textbox and click the button, the correct view is displayed.
My long term goal is to have a database, with a couple of tables. A table for "MenuItems" and a table for "Views". Instead of using buttons, I'll use the menu control. Then once a menu item is selected, it would display the correct view.
But for now, I'm startings small and trying to keep it simple:
--------- WPF - XAML START ---------------------------
<StackPanel Grid.Column="0" Orientation="Vertical">
<TextBox x:Name="txtDisplayView" Height="23" Margin="5" TextAlignment="Center"/>
<Button x:Name="btnDisplayView" Content="Display" Margin="5" Click="btnDisplay_Click"/>
<Button x:Name="btnRed" Content="Red" Margin="5" DataContext="RedView" Click="Red_Click"/>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Vertical">
<ContentControl Margin="5" Content="{Binding}"/>
</StackPanel>
-----------WPF - XAML END -------------------------
If someone could show me how to get this to work, it would help me move my project in the right direction and would be greatly appreciated.
What you need here is:
Create a property in your DataContext that represents the selected item
Bind that property to your TextBox element
Now, you have two options. One is "WPF Friendly" and the other is more Windows Forms-ish:
Create a command (take a look at this article) that reads a parameter, which will be binded to the property you created before
On the Click event, you can read the binded property value
I personally prefer the first solution. Why? Because when you change it to a Menu, for example, your work will be only to populate the menu with your list items (the MenuItem class also has a Command property, so the implementation is the same as with a Button). You will only need to change the source!
I've got a simple WPF ComboBox, displaying Orders/Positions on the Financial Markets.
<ComboBox Name="TradeDropDown"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
ItemsSource="{Binding Path=ActiveOrders}"
DisplayMemberPath="OrderLabel"
SelectedItem="{Binding Path=SelectedOrder, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" />
I need to see at a glance how many items are in the list. I've added a TextBlock above with summary information.
I don't like it, and would prefer to have the items in the dropdown listed like:
(1/2) Working Short 425K
(2/2) Filled Long 979K
etc - and have the 1/2 numbers correctly update as items are added and removed from the list.
The Items are stored in a BindingList.
Is there an easy way to do this?
Is there an easy way to do this?
Add another property to the class where the OrderLabel property is defined that returns a string like "(1/2) Working Short 425K" and set the DisplayMemberPath property of the ComboBox to the name of this property.
Make sure that the class implements the INotifyPropertyChanged interface.
You then set the new property to a new value and raise the PropertyChanged event whenever you want to update the label in the ComboBox.
I'm using a telerik RadGridView which is pretty much the same thing as a normal DataGrid in WPF. In my gridview.columns I have a GridViewDataColumn which then allows me to put a celltemplate then a datatemplate and then allow me to put different controls within a grid. I have a combobox and a textbox(only one shows at a time based on visibility property). The problem I'm having is the tab system is kind of weird and doesn't work right. When I tab to a cell in the column above, my combobox nor my textbox ever gets focus. In fact the cell turns completly white. So I was wondering how (in code behind) can I detect when a user tabs in this particular cell and manually set focus to these child elements inside this cell on the selected row?
<telerik:GridViewDataColumn x:Name="MyDataColumn" Focusable="True" GotFocus="MyDataColumn_GotFocus_1" Header="Header1" Width="250">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<Textbox x:name="MyTextbox" Visibility="{Binding IsTextbox}"/>
<Combobox x:name="MyCombobox" Visibility="{Binding IsCombo}"/>
</Grid>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
*Basically, how can I gain access to one of those child controls inside this GridViewDataColumn in code behind so that I can set focus to it? Thanks so much for any advice.
Probably the most straight forward answer to your question can be found by reading the answer to the Access items inside the DataTemplate in WPF post.
However, it may be worth reading the correct answer in this Access Elements inside a DataTemplate… How to for more than 1 DataTemplate? post also.
Anyone has any idea how to highlight in a textblock?
Basically i have 2 textblock and both have the same string. When I highlight part of the string in one of the textblock, the other textblock shows the same highlighted part as well. I am basically stuck at how to do the highlighting.
Thanks in advance
Do you have a TextBlock or a TextBox? I'm going to assume it's a TextBox, since TextBlock does not support text selection.
In that case, you can simply use data binding to keep this in sync.
<TextBox Name="text1" />
<TextBox Name="text2"
SelectionStart="{Binding Path=SelectionStart, ElementName=text1}"
SelectionLength="{Binding Path=SelectionLength, ElementName=text1}" />
This should ensure that the same area of text is selected in text2 when the user selects it in text1 and vice-versa.
EDIT See this answer for instructions on how to bind to these properties.
If you create a custom TextBox as described in the linked answer, your code would look something like this:
<SelectionBindingTextBox Name="text1" />
<SelectionBindingTextBox Name="text2"
BindableSelectionStart="{Binding Path=BindableSelectionStart, ElementName=text1}"
BindableSelectionLength="{Binding Path=BindableSelectionLength, ElementName=text1}" />