Get ListView control values in combobox selection changed Event - wpf

I am adding Label and Combo box dynamically to ListView control in combo box Selection Changed Event.
I want to get label content for the selected list view Item

You can use VisualTreeHelper to navigate Label from the SelectedItem;
or
Simple Way, you can do like below(but do add null checks where ever necessary);
XAML Code:
<StackPanel>
<ListView SelectionChanged="Selector_OnSelectionChanged">
<ListViewItem>
<StackPanel>
<Label Content="Label1"/>
<ComboBox>
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
</ComboBox>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel>
<Label Content="Label2"/>
<ComboBox>
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
</ComboBox>
</StackPanel>
</ListViewItem>
</ListView>
<TextBlock x:Name="TextBlock1" FontSize="25"/>
</StackPanel>
C# Code:
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string content =
((((e.AddedItems[0] as ListViewItem).Content) as StackPanel).Children[0] as
Label).Content.ToString();
TextBlock1.Text = "Selected Item's Label content is :" + content;
}
For simple solution, I have navigated programmatically;
((((e.AddedItems[0] as ListViewItem).Content) as StackPanel).Children[0] as Label).Content

Related

ListView SelectionChanged won't update when enclosed TextBox clicked

I have a list view with a gridview. The gridview second column contains textboxes (and in case this is relevant to this particular problem, the textboxes are rendered by a datatemplate).
I want the listview SelectedItem to update when the user clicks in the textbox to type and reflect the row (or listview item) containing the textbox. Right now listview SelectedItem will update only if the user's click on a row is just outside the textbox. If I click the textbox or type in it, the highlighted row stays the same and no SelectionChanged occurs for the listview.
How can I get the listview's selecteditem to update? Can I maybe force a change in its selecteditem or enable the listview to see the textbox click as its own somehow?
Thanks in advance.
P.s. Solution located here:
https://drive.google.com/file/d/0B89vOvsI7UbdTmN5RVdLVTFGeXM/view?usp=sharing
ListView xaml:
<ListView x:Name="CombinedlistView" Grid.Column="0"
DataContext="{Binding ElementName=mainWindow}"
SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView >
<GridViewColumn CellTemplate="{StaticResource labelTemplate}" />
<GridViewColumn CellTemplateSelector="{StaticResource fieldValueTemplateSelector}" />
</GridView>
</ListView.View>
</ListView>
TemplateSelector:
<loc:FieldValueTemplateSelector x:Key="fieldValueTemplateSelector"
StringTemplate="{StaticResource stringTemplate}"
DateTimeTemplate="{StaticResource dateTimeTemplate}"/>
DataTemplate where textbox is located:
<DataTemplate x:Key="stringTemplate" DataType="{x:Type System:String}">
<StackPanel Style="{StaticResource dataTemplateStackPanelStyle}" Name="stringTemplStack">
<TextBox Name="searchValText" Width="120"
Text="{Binding Path=TextVal, Mode=OneWayToSource}"
DataContext="{Binding ElementName=mainWindow}"/>
</StackPanel>
</DataTemplate>
try wiring up this event to your PreviewGotKeyboardFocus event on your listviewitem:
protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
{
ListViewItem item = (ListViewItem)sender;
item.IsSelected = true;
}

ListBox and Checkbox - XAML/C# - Windows 8 App

I have a little problem with my Project. I have an ListBox with a Checkbox for each item. The Checkbox takes the Content from ListBox, but how do I connect the Selection of the Item and the IsChecked from the Checkbox?
My plan is that when you Check the Checkbox that the line with the Checked Checkbox will be deleted. How do I do that?
My XAML so far:
<ListBox x:Name="To_do_Liste" ItemsSource="{Binding}" Height="593"
Margin="0,85,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5"
BorderBrush="#FDFFE818" Background="#FFFFE818"
IsSynchronizedWithCurrentItem="False" IsDoubleTapEnabled="True"
IsHoldingEnabled="True" IsRightTapEnabled="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" ManipulationMode="All"
FontFamily="SketchFlow Print" HorizontalAlignment="Left" Width="320"
SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem HorizontalContentAlignment="Stretch">
<CheckBox x:Name="CheckBox1" Foreground="Black"
BorderBrush="#FF007FFF" Content="{Binding}" FontFamily="SketchFlow Print"
FontSize="26" />
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
you can add selectionchange eventhandler to listbox and here you can check which checkbox is selected either using selectedindex of some other property of checkbox and now update your OBSERVABLECOLLECTION to with you listbox is binded.
private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
CheckBox chkbx = sender as CheckBox;
}

How can I access information in the selected row of a XAML gridview?

I have a listview with a gridview inside it and it is bound to a list of custom objects. In one cell I have three elements that I swap between, one of which is a hyperlink with a click event.
How do I get access to the 'CompanyName' that is bound in the same row when I go to the click event on the hyperlink?
This question may bely my ASP.Net background - I am very new to WPF.
<GridViewColumn Header="File" Width="100" DisplayMemberBinding="{Binding Path=CompanyFile}"/>
<GridViewColumn Header="Action" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Status}" Visibility="{Binding Path=StatusVisibility}"></TextBlock>
<TextBlock Visibility="{Binding Path=ButtonVisibility}"><Hyperlink Click="Hyperlink_Click"><TextBlock Text="{Binding Path=Button}"></TextBlock></Hyperlink></TextBlock>
<ProgressBar Value="{Binding Path=Progress}" Visibility="{Binding Path=ProgressVisibility}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The Hyperlink inherits the DataContext of the parent ListViewItem, so you just need to cast it:
void Hyperlink_Click(object sender, EventArgs e)
{
Hyperlink link = (Hyperlink)sender;
MyCustomObject obj = (MyCustomObject)link.DataContext;
string companyName = obj.CompanyName;
...
}
Another option is to bind the hyperlink's Command property to a command on your DataContext, rather than handling the Click event explicitly. This helps decoupling the view from the business-related code, and it's the usual way of doing things in the MVVM pattern.

Databinding and bound buttons

I have an ItemsControl bound to a list of objects.
It basically displays a list of bound properties on screen with a textbox and button next to each one to allow you to add additional information to each database field.
In the onclick of the button I need to take the string in the textbox and store it in the object for that item. The XAML looks a bit like this
<ItemsControl x:Name="ListDatabaseFields" ItemsSource="{Binding Path=SelectedImport.ColumnMappings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding DatabaseColumn.Name}" TextWrapping="Wrap" Grid.Column="0"/>
<TextBox Name="txtNewFileField" Width="100"/>
<Button Name="Add" Content="Add File Field" Style="{StaticResource LinkButton}" Width="50" Click="Add_Click"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Whats the best way in the XAML to access the ColumnMapping the itemcontrol is bound to for that item and change its file field property.
I'm not sure if i understood your question correctly, please clarify if this is not what you are looking for.
I have adapted this to my test application, my DataTemplate looks like this:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBox MinWidth="100" Name="tbNewName"/>
<Button MinWidth="100"
Tag="{x:Reference tbNewName}" Click="ButtonNewName_Click"
Content="Do Stuff"/>
</StackPanel>
</DataTemplate>
Notice the Button.Tag which references the TextBox, i can use it in the handler like this:
private void ButtonNewName_Click(object sender, RoutedEventArgs e)
{
Employee emp = (sender as FrameworkElement).DataContext as Employee;
TextBox tbNewName = (sender as FrameworkElement).Tag as TextBox;
emp.Name = tbNewName.Text;
}
The DataContext is inherited, so the sender (the Button) contains the data-item, in your case a ColumnMapping, the Tag gives me the TextBox and that is all i need for changing a property.
If you need to reference more controls you could create an array in the Tag.

Silverlight: How to dynamically bind a ComboBox in a ListBox ItemTemplate?

I have a list box that requires at least one ComboBox. I couldn't find a way to place the ComboBox in the ItemTemplate I use.
...
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox .../>
</StackPanel>
</DataTemplate>
...
<ListBox x:Name="lb_parts" ItemTemplate="{StaticResource parts_template}" .../>
...
How do bind that ComoBox in the DataTemplate to an ObservableCollection in the code behind?
Another thing you could try is subscribe the Loaded event on the ComboBox.
Then you can set the ComboBox.ItemsSource in the EventHandler to MyObservableCollection.
Have a look
XAML:
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox Loaded="ComboBox_OnLoaded">
<!-- ComboBox ItemTemplate -->
</ComboBox>
</StackPanel>
</DataTemplate>
C# Code Behind:
private void ComboBox_OnLoaded(object sender, EventArgs e)
{
((ComboBox)sender).ItemsSource = MyObservableCollection;
}
Okay, here is how you can add a ComboBox to the ListBox in the code behind.
Create a ComboBox
ComboBox x = new ComboBox();
If you have a data source that populates the ComboBox then you can just bind that
x.ItemsSource = e.Result;
If you do not and want to manually add items to the ComboBox:
ComboBoxItem y = new ComboBoxItem();
Set the Content of the Item to what you want displayed in the ComboBox
y.Content = "Hello";
Now all that is left, is to add the ComboBoxItem to the ComboBox (only if you are creating the Items manually), and then the ComboBox to the ListBox
x.Items.Add(y);
//testing is my ListBox
testing.Items.Add(x);
You should be able to set the data context to the List itself
lb_Parts.DataContext=myCollection;
Then you should be able to bind to it in the template
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox ItemSource={Binding}/>
</StackPanel>
</DataTemplate>

Resources