How to Binding ListBox selected item and TextBox? - wpf

I have listBox and i want to see the selected item ftom the listBox on some textBox.
(the Collection is a list of string)
I trying to write the code but this is not working.
<ListBox x:Name="Collection__" Grid.Row="0" ItemsSource="{Binding Collection}" />
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="5">
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="Collection name:"/>
<TextBox DockPanel.Dock="Left" Margin="5,0" Text="{Binding ElementName=Collection__, Path=SelectedItem}"/>
</DockPanel>
</StackPanel>

found it ...
<StackPanel>
<ListBox x:Name="ElementListBox" ItemsSource="{Binding Elements}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Text="{Binding Elements/Name}"/>
</StackPanel>

Related

WPF:- How to create a Treeview control with combo box, check box and text box in it using MVVM

I'm trying to put a ComboBox, a checkbox and a few textboxes inside a treeview in WPF.
Following is the basic structure of the treeview requirement
-Parent
--Label Textbox
--Label Textbox
--Label Textbox
--Label Combo box
--Label Check box
--Payload
---Label
----Label Textbox
----Label Combobox
----Label Textbox
Following is the xaml code
<TreeView x:Name="BTreeView" Grid.Column="1" Grid.Row="1" Margin="1,10,0,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding TreeViewListings}" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewmodel:TreeViewBViewModel}" ItemsSource="{Binding Positions}" >
<TextBlock Text="Level1"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding PayLoadList}" DataType="{x:Type viewmodel:Position}">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 1" />
<TextBox Text="{Binding Path=Text1, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="10,0,0,0" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 2" />
<TextBox Text="{Binding Path=Text1, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="10,0,0,0" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 3"/>
<ComboBox Width="100" Height="18" Margin="10,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="10" IsEditable="True" IsReadOnly="True" BorderThickness="0">
<ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
<ComboBoxItem>Item 5</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<CheckBox Canvas.Left="10" Canvas.Top="10" Content="Active" IsChecked="False"/>
</StackPanel>
<TextBlock Text="{Binding PayLoadText}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding PayLoadData}" DataType="{x:Type viewmodel:Payload}">
<Label Content="{Binding FieldName}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type viewmodel:PayloadData}">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 4" />
<TextBox Text="{Binding Path=Text4, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="10,0,0,0" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Data}" />
<TextBox Text="{Binding Path=DataFieldValue, UpdateSourceTrigger=PropertyChanged}" Width="200" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Operation" />
<ComboBox Width="100" Height="18" Margin="10,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="10" IsEditable="True" IsReadOnly="True" BorderThickness="0">
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
</ComboBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
I am using MVVM architectural pattern to achieve this goal. The problem that I am facing is with the arrow head of second level node. It is not aligned at the right position. The arrowhead gets automatically aligned to "Text3" combo box instead of "Payload" node. Following is the image of my tree view
I do not know if there is any other approach to include Combo box, check box, text boxes inside a treeview using MVVM approach. Would appreciate if you let me know your approach of designing this treeview

How to get value of textblock in mvvm

I have an ItemControl containing a list of elements.
I need to get the value of the TextBlock using MVVM.
This is my XAML code:
<ItemsControl ItemsSource="{Binding IhmtEcosystemFilterList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="5" Height="80" Background="LightBlue" BorderBrush="Black" Command="{Binding CmdStatistiqueEcoSystem, ElementName={Binding Name}}">
<StackPanel Width="150">
<TextBlock Text="{Binding Name}" Foreground="White" FontSize="17" Name="valueTxtBloc" />
<Image x:Name="imgAvion" Source="/Resources/Images/avion.png" Width="35"></Image>
</StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>

ComboBox TextWrap Binding

I have the following ComboBox
<ComboBox x:Name="TaskText" Text="{Binding TaskNameBinding}" ItemsSource="{Binding
taskList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name"
Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="90"/>
I wish to apply Text Wrapping to this combobox and followed to code snippet from the answer here
<ComboBox x:Name="TaskText" ItemsSource="{Binding taskList, ElementName=MainWin}"
SelectedValuePath="_id" DisplayMemberPath="_name" Background="Yellow" Padding="0"
Margin="0" BorderThickness="0" Width="90">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskNameBinding}"
TextTrimming="CharacterEllipsis" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But this template is breaking the binding and the combobox displays no values. Any help would be appreciated
Figured it out
<ComboBox x:Name="TaskText" Text="{Binding TaskNameBinding}" ItemsSource="{Binding taskList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="90">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding _name}"
TextWrapping="Wrap" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Binding data to the Listbox where ListBox has textblocks

<ListBox Height="498" Margin="2,0,0,0" Name="listBox1" Width="879" ItemsSource="{Binding}" >
<ListBoxItem >
<StackPanel Width="418" Orientation="Horizontal">
<TextBlock Name="MedicineName" Text="Alamoxy"
FontWeight="Bold" FontSize="18"
Margin="5" Width="205" >
</TextBlock>
<TextBlock Name="ListBoxLetter" Text="Amoksilin"
FontSize="18" Margin="0" Width="255" Height="23">
</TextBlock>
</StackPanel>
</ListBoxItem>
</ListBox>
I want to bind data to the listbox.
Textblock will show seprate fields. How i can do it?
please Help me about this/
Use an ItemTemplate to make it look something like this.
<ListBox Width="400" Margin="10" ItemsSource="{Binding myItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=MedicineName}" />
<TextBlock Text="{Binding Path=ListBoxLetter}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

How can I make a ListBox expand to fit the entire available width?

I need to make the ListBox expand to fit the width it has available. Here's the XAML:
<ListBox Grid.Row="2" Height="400" ItemsSource="{StaticResource Notes}" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Height="90" Margin="5">
<TextBlock Text="{Binding Title}" FontSize="30" />
<TextBlock Text="{Binding Text}" FontSize="15" />
<Rectangle Fill="#1192D4" Height="2" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Please take a look at this post
A ListBoxItem that fills its parent

Resources