how do i make a collapsed tab item visible programmatically in WPF?
I have created a xaml file with a tabcontrol with three tabs, the visibility property I have set succesively to Visible. Hidden and Collapsed. Also I have two buttons one to set Tab 2 to visible and the other to set Tab 3 to visible.
In the code behind I have set up an event handler for each button.
The event handlers don't compile.
I would appreciate any suggestions . Thanks!
Here is my MainWindow.xaml
<Window x:Class="WPFTabItemVisibility.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TabControl>
<TabItem Header="Tab 1" Visibility="Visible" Content="Tab 1 Content goes here"></TabItem>
<TabItem Header="Tab 2" Visibility="Hidden" Content="Tab 2 Content goes here"></TabItem>
<TabItem Header="Tab 3" Visibility="Collapsed" Content="Tab 3 Content goes here"></TabItem>
</TabControl>
<Button Content="Make Tab 2 Visible" Margin="20" Height="30" Width="200" Click="Button_Click"/>
<Button Content="Make Tab 3 Visible" Margin="20" Height="30" Width="200" Click="Button_Click_1"/>
</StackPanel>
</Window>
here is my MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFTabItemVisibility
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TabIndex = 1;
TabItem.VisibilityProperty = IsVisible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
TabIndex = 2;
TabItem.VisibilityProperty = IsVisible;
}
}
}
Thank you!
You can give name to the tabitem and set its visibility. Refer the below code.
<TabControl>
<TabItem x:Name="tab1" Header="Tab 1" Visibility="Visible" Content="Tab 1 Content goes here"></TabItem>
<TabItem x:Name="tab2" Header="Tab 2" Visibility="Hidden" Content="Tab 2 Content goes here"></TabItem>
<TabItem x:Name="tab3" Header="Tab 3" Visibility="Collapsed" Content="Tab 3 Content goes here"></TabItem>
</TabControl>
private void Button_Click(object sender, RoutedEventArgs e)
{
tab2.Visibility = Visibility.Visible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
tab3.Visibility = Visibility.Visible;
}
try this in your XAML
<StackPanel>
<TabControl>
<TabItem Header="Tab 1"
Visibility="Visible"
Name="tab1"
Content="Tab 1 Content goes here"></TabItem>
<TabItem Header="Tab 2"
Visibility="Hidden"
Name="tab2"
Content="Tab 2 Content goes here"></TabItem>
<TabItem Header="Tab 3"
Name="tab3"
Visibility="Collapsed"
Content="Tab 3 Content goes here"></TabItem>
</TabControl>
<Button Content="Make Tab 2 Visible"
Margin="20"
Height="30"
Width="200"
Click="Button_Click" />
<Button Content="Make Tab 3 Visible"
Margin="20"
Height="30"
Width="200"
Click="Button_Click_1" />
</StackPanel>
CodeBehind .cs File
private void Button_Click(object sender, RoutedEventArgs e)
{
//TabIndex = 1;
//TabItem.VisibilityProperty = IsVisible;
tab2.Visibility = Visibility.Visible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//TabIndex = 2;
//TabItem.VisibilityProperty = IsVisible;
tab3.Visibility = Visibility.Visible;
}
Related
I am extremely new using C# and that's far away from my mechanical skills ... I have very basic knowledge in C# but I am really willing to push the knowledge effort. Thanks to keep it in minde.
I creating some sort of application/ERP to help with working with my suppliers. The final goal is to create a list of suppliers, their skills, consult RFQ, update RFQ, send emails, ... I have been doing it in Excel VBA in the past but now I am willing to build up something stronger (and also willing to learn something new ...).
First of all, I am willing to create a ListBox with different kind of processes. I would like this ListBox to only show one ListBoxItems (one row) and when the mouse is entering into the ListBox, grow the ListBox to it's full size.
(My goal would be to ! create a ListBox with a dropdown for multiselection :=))
The dirty way I was thinking doing it is:
Create a TextBox with "Click to expand" text. When the mouse is entering the Textbox, hide it and change the listbox visibility to Visible (default visibility to Hidde);
Any thought about it ? Where should I start by ?
I will then selected multiple items in my ListBox.
Latter on, I would like to binding ListBox content to a data base (don't know how to create it on my computer).
Any help is greatly appreciated !
Cheers
Edit:
Below is my XAML code:
<Window x:Class="ERP_Fournisseurs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ERP_Fournisseurs"
mc:Ignorable="d"
Title="ERP Fournisseurs" Height="450" Width="800">
<Canvas>
<TabControl Height="369" Width="734" Canvas.Left="10" Canvas.Top="9">
<TabItem Header="Ajouter fournisseurs">
<Canvas Margin="0,0,0,-1">
<TextBlock Canvas.Left="10" Name="Fournisseur" TextWrapping="Wrap" Text="Nom fournisseur" Canvas.Top="16" />
<TextBox Height="23" Name="NomTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer nom fournisseur" Canvas.Top="14" Width="225" MouseDoubleClick="NomFournisseur_MouseDoubleClick" TextChanged="NomTXT_TextChanged" />
<TextBlock Canvas.Left="10" Name="Telephone" TextWrapping="Wrap" Text="Telephone" Canvas.Top="55"/>
<TextBox Height="23" Name="TelTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer téléphone fournisseur" Canvas.Top="55" Width="225" MouseDoubleClick="TelTXT_MouseDoubleClick"/>
<TextBlock Canvas.Left="10" Name="Mail" TextWrapping="Wrap" Text="Mail" Canvas.Top="84"/>
<TextBox Height="23" Name="MailTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer adresse e-mail" Canvas.Top="91" Width="225" MouseDoubleClick="MailTXT_MouseDoubleClick"/>
<TextBlock Canvas.Left="10" Name="Adresse" TextWrapping="Wrap" Text="Adresse" Canvas.Top="124"/>
<TextBox Height="23" Name="AdresseTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer adresse" Canvas.Top="127" Width="225" MouseDoubleClick="AdresseTXT_MouseDoubleClick"/>
<TextBlock Canvas.Left="10" Name="Types" TextWrapping="Wrap" Text="Types" Canvas.Top="157"/>
<TextBox Height="23" Name="TypesTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer types" Canvas.Top="162" Width="225" />
<TextBlock Canvas.Left="10" Name="Procédés" TextWrapping="Wrap" Text="Procédés" Canvas.Top="194"/>
<TextBlock Canvas.Left="10" Name="Matériaux" TextWrapping="Wrap" Text="Matériaux" Canvas.Top="232"/>
<TextBlock Canvas.Left="10" Name="Délais" TextWrapping="Wrap" Text="Délais" Canvas.Top="258"/>
<TextBlock Canvas.Left="10" Name="Autres" TextWrapping="Wrap" Text="Autres" Canvas.Top="289"/>
<TextBox Height="23" x:Name="AutreTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer types" Canvas.Top="289" Width="225" MouseDoubleClick="AutreTXT_MouseDoubleClick"/>
<Button Name="AjoutTXT" Content="Ajouter" Height="100" Width="75" Canvas.Left="563" Canvas.Top="107" Click="AjoutTXT_Click"/>
<ListBox Name="ListBoxProc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="181" Height="186" Canvas.Left="409" Canvas.Top="54" Visibility="Hidden" MouseLeave="ListBoxProc_MouseLeave" MouseEnter="ListBoxProc_MouseEnter" SelectionMode="Multiple">
<ListBoxItem>TEST 1</ListBoxItem>
<ListBoxItem>TEST 1</ListBoxItem>
<ListBoxItem>TEST 1</ListBoxItem>
<ListBoxItem>TEST 1</ListBoxItem>
</ListBox>
<TextBox Height="23" Name="ProcTXT2" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer types" Canvas.Top="196" Width="225" />
</Canvas>
</TabItem>
<TabItem Header="Suivi devis" >
<Canvas>
</Canvas>
</TabItem>
<TabItem Header="Liste fournisseurs" >
<Canvas>
</Canvas>
</TabItem>
</TabControl>
</Canvas>
</Window>
Below is my XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ERP_Fournisseurs
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void NomFournisseur_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
NomTXT.Text = "";
}
private void TelTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TelTXT.Text = "";
}
private void MailTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MailTXT.Text = "";
}
private void AdresseTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
AdresseTXT.Text = "";
}
private void AjoutTXT_Click(object sender, RoutedEventArgs e)
{
}
private void AutreTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
AutreTXT.Text = "";
}
private void ProcTXT_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void NomTXT_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void MaterTXT_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void ProcTXT_MouseEnter(object sender, MouseEventArgs e)
{
}
private void ListBoxProc_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxProc.Visibility = Visibility.Hidden;
}
private void ListBoxProc_MouseLeave(object sender, MouseEventArgs e)
{
ListBoxProc.Visibility = Visibility.Visible;
}
//https://www.c-sharpcorner.com/UploadFile/mahesh/listbox-in-wpf/ Binding ListBox to Data Base
}
}
What I have tried:
Asked google my solution, but it's answerless;
Tried the following code (https://www.codeproject.com/Questions/696480/How-to-make-a-ListBox-grow-on-mouseover) but not working
I want to find a control within a TabItem. Using FindName it is not possible. I found a way traversing the visual tree but that looks rather cumbersome. Why does FindName not do the job?
XAM:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button
Content="Click"
Click="Button_Click"
/>
<TabControl Grid.Row="1" x:Name="tabControl">
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox
x:Name="textBox"
Text="{Binding DataContext.Message, ElementName=mainWindow}"
/>
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
<TabItem Header="One" />
<TabItem Header="Two" />
<TabItem Header="Three" />
</TabControl>
</Grid>
Code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TabControl tabControl = FindName("tabControl") as TabControl;
TextBox textBox = FindName("textBox") as TextBox;
TabItem tabItem = tabControl.SelectedItem as TabItem;
textBox = tabItem.FindName("textBox") as TextBox;
}
}
"textBox" is null regardless whether searched from top or from selected tabItem.
The TextBox is not a visual child of the TabItem. If you look at the visual tree, you'll see that the contents of the currently selected tab is hosted in a ContentPresenter that is part of the TabControl's ControlTemplate.
This should work though:
private void Button_Click(object sender, RoutedEventArgs e)
{
ContentPresenter cp = tabControl.Template.FindName("PART_SelectedContentHost", tabControl) as ContentPresenter;
TextBox textBox = cp.ContentTemplate.FindName("textBox", cp) as TextBox;
}
My purpose is that: double-click the TabItem named "Tab" in the TabControl, then the TabControl will be folded, another double-click lead to unfolded.
But after I move the GridSplitter and double-click the "Tab" to be folded, it don't work correctly that the the height of the column the TabControl in isn't equal to the height of the TabControl, in other word, GridSplitter don't follow the "TabControl".
.xaml
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="20"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid Grid.Column="1">
<Label>
Nothing
</Label>
</Grid>
</Grid>
<GridSplitter Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Height="6" />
<TabControl Grid.Row="2"
TabStripPlacement="Bottom"
VerticalAlignment="Stretch">
<TabItem Header="Tab"
MouseDoubleClick="TabItemDoubleCilck">
<!--ListView-->
<ListView Grid.Row="1"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ListView.View>
<GridView>
<GridViewColumn Header="AA "
Width="100"
DisplayMemberBinding="{Binding [0]}" />
<GridViewColumn Header="BB"
Width="100"
DisplayMemberBinding="{Binding [1]}" />
<GridViewColumn Header="CC"
Width="100"
DisplayMemberBinding="{Binding [2]}" />
<GridViewColumn Header="DD"
Width="100"
DisplayMemberBinding="{Binding [3]}" />
</GridView>
</ListView.View>
<ListViewItem>
<x:Array Type="sys:String" >
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
</x:Array>
</ListViewItem>
<ListViewItem>
<x:Array Type="sys:String" >
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
</x:Array>
</ListViewItem>
</ListView>
</TabItem>
</TabControl>
</Grid>
</Window>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication7
{
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
const double dBottomTabMinHeight = 20.0;
private bool isBottomTabUnfold;
public MainWindow()
{
InitializeComponent();
isBottomTabUnfold = true;
}
private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
{
TabItem tabItm = sender as TabItem;
if (isBottomTabUnfold)
{
((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
}
else
{
((FrameworkElement)tabItm.Parent).Height = Double.NaN;
}
isBottomTabUnfold = !isBottomTabUnfold;
}
}
}
try this, hope this is what you expected
private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
{
TabItem tabItm = sender as TabItem;
if (isBottomTabUnfold)
{
((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
((FrameworkElement)tabItm.Parent).VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
}
else
{
((FrameworkElement)tabItm.Parent).Height = Double.NaN;
((FrameworkElement)tabItm.Parent).VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
}
isBottomTabUnfold = !isBottomTabUnfold;
}
edit:
like this then?
private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
{
TabItem tabItm = sender as TabItem;
if (isBottomTabUnfold)
{
dUnfoldedHeight = ((FrameworkElement)tabItm.Parent).ActualHeight;
((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
rowTab.Height = new GridLength(1, GridUnitType.Auto);
}
else
{
((FrameworkElement)tabItm.Parent).Height = double.NaN;
rowTab.Height = new GridLength(dUnfoldedHeight);
}
isBottomTabUnfold = !isBottomTabUnfold;
}
NB:
rowTab is the name of the 3rd rowdefinition, dUnfoldedHeight is a private member to track the height of the unfolded tab before it's folded.
you might find a weird behavior on dragging the splitter up when the tab is folded or when dragging the spliiter down until the tab is look like unfolded, to fix this you probably have to work with the DragDelta and DragCompleted event of the splitter to decide whether in the end the tab should be considered as folded or not
i would like to change to a different tab when i click a button.
my xaml is as follow:
<TabControl Name="Tabcontrol1" Margin=" 5" SelectedIndex="0">
<TabItem Header="Properties" Opacity="1">
<Grid Width="1185" Height="945" Background="Snow" >
</Grid>
</TabItem>
<TabItem Header="Others">
<Grid>
</Grid>
</TabItem>
</TabControl>
and my button click event:
private void BuildButton_Click(object sender, RoutedEventArgs e)
{
Tabcontrol1.SelectedIndex = "1";
}
is there something wrong? "Cannot implicitly convert type 'string' to 'int'" appears
Remove quotes:
Tabcontrol1.SelectedIndex = 1;
XAML:
<Window x:Class="WpfApp_ListBoxTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ListBox Name="lb" Margin="0,0,0,70"></ListBox>
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,41" Name="btnAdd" VerticalAlignment="Bottom" Content="Add item" Width="75" Click="btnAdd_Click"></Button>
<TextBox Height="23" Margin="93,0,12,41" Name="txtInput" VerticalAlignment="Bottom" />
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,12" Name="btnGet" VerticalAlignment="Bottom" Content="Get value" Width="75" Click="btnGet_Click"></Button>
<TextBox Height="23" Margin="93,0,12,12" Name="txtReturn" VerticalAlignment="Bottom" IsReadOnly="True" />
</Grid>
</Window>
Csharp:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
namespace WpfApp_ListBoxTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
TextBox txt = new TextBox();
txt.Width = 200;
txt.Text = txtInput.Text;
lb.Items.Add(txt);
}
private void btnGet_Click(object sender, RoutedEventArgs e)
{
// What do I need to write here to get the value of the Text property of the selected TextBox?
}
}
}
And screenshot: (Sorry I'm not allowed to post picture directly)
http://i825.photobucket.com/albums/zz180/mGlushed/get_listbox_item_property.png
(In the picture above, I want to get the value "b" when I click the "Get value" button.)
I would like to know if there is a simple way to achieve this.
I'm new to WPF, so I only know to do this the long way, which is: Create an array. Everytime a new TextBox is created, add it into the array. Then access the TextBox'es through the array. But that doesn't sound very optimal, I think.
The 'WPF Way' of doing what you want is to use data binding:
Define a class with a string property called Text.
Create a collection of that class.
Bind your list box ItemsSource to the collection.
Create a DataTemplate that shows a TextBox with its Text property bound using {Binding Path=Text}.
In btnAdd_Click add an item to the collection (not directly to the ListBox)
In btnGet_Click you can get the text entered by casting ListBox.SelectedItem to your class and getting its Text property.
Example:
The simple class:
public class VMObject
{
public VMObject(string text)
{
Text = text;
}
public string Text { get; set; }
}
The window code-behind:
public partial class Window1 : Window
{
public ObservableCollection<VMObject> VM { get; set; }
public Window1()
{
VM = new ObservableCollection<VMObject>();
InitializeComponent();
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
VM.Add(new VMObject(txtInput.Text));
}
private void btnGet_Click(object sender, RoutedEventArgs e)
{
if (lb.SelectedItem == null)
MessageBox.Show("No item is selected!");
txtReturn.Text = ((VMObject)lb.SelectedItem).Text;
}
}
The XAML:
<Window x:Class="lbtest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="Window"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="TextBoxTemplate">
<TextBox Text="{Binding Path=Text}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Name="lb" Margin="0,0,0,70"
ItemsSource="{Binding ElementName=Window, Path=VM}"
ItemTemplate="{StaticResource TextBoxTemplate}" />
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,41"
Name="btnAdd" VerticalAlignment="Bottom"
Content="Add item" Width="75" Click="btnAdd_Click" />
<TextBox Height="23" Margin="93,0,12,41"
Name="txtInput" VerticalAlignment="Bottom" />
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,12"
Name="btnGet" VerticalAlignment="Bottom"
Content="Get value" Width="75" Click="btnGet_Click" />
<TextBox Height="23" Margin="93,0,12,12"
Name="txtReturn" VerticalAlignment="Bottom" IsReadOnly="True" />
</Grid>
</Window>
for a checkbox item:
private void chk_Checked(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
MessageBox.Show(chk.Content.ToString());
}
No need for TextBox:s. ListBox handle strings fine.
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
// No need to create TextBox, ListBox handle strings fine.
lb.Items.Add(txtInput.Text);
}
private void btnGet_Click(object sender, RoutedEventArgs e)
{
// No selection, but button has been pressed.
if(lb.SelectedItem == -1)
return;
// Get selected item.
txtReturn.Text = (string)lb.SelectedItem;
/* If you change ListBox selection mode to multiple
* you can get all selected items by using foreach loop.
foreach (Object selected in lb.SelectedItems)
{
txtReturn.Text += (string) selected;
}
*/
}
If you just want to get the Text property of the selected TextBox (admiting your ListBox is in single selection mode) it it quite simple:
private void btnGet_Click(object sender, RoutedEventArgs e)
{
if(lb.SelectedItem != -1)
{
TextBox selectedTextBox = (TextBox)lb.SelectedItem;
txtReturn.Text = selectedTextBox.Text;
}
}
But if you want to implement the pretty WPF way, you should follow the Aviad P. solution, my solution do it well too.
Regards.
EDIT: If do not have a real need of TextBox functionalities, but only a string container, so follow Tuukka's solution.