listbox binding
This commit is contained in:
@@ -25,4 +25,12 @@
|
||||
</Resource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Resource.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resource.resx</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@@ -38,10 +38,12 @@
|
||||
<Border Grid.Column="0" Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" Background="LightGray">
|
||||
<Label Content="Sex(_S):" HorizontalAlignment="Center" VerticalAlignment="Center" Target="{Binding ElementName=cboSex}"/>
|
||||
</Border>
|
||||
<ComboBox Grid.Column="1" Grid.Row="2" x:Name="cboSex" Width="150" HorizontalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" SelectedIndex="0">
|
||||
<ComboBoxItem Content="Male"/>
|
||||
<ComboBoxItem Content="Female"/>
|
||||
</ComboBox>
|
||||
<Border Grid.Column="1" Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1">
|
||||
<ComboBox Grid.Column="1" Grid.Row="2" x:Name="cboSex" Width="150" HorizontalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" SelectedIndex="0">
|
||||
<ComboBoxItem Content="Male"/>
|
||||
<ComboBoxItem Content="Female"/>
|
||||
</ComboBox>
|
||||
</Border>
|
||||
|
||||
<Border Grid.Column="0" Grid.Row="3" BorderBrush="DarkGray" BorderThickness="1" Background="LightGray">
|
||||
<Label Content="Note(_N):" HorizontalAlignment="Center" VerticalAlignment="Center" Target="{Binding ElementName=txtNote}"/>
|
||||
|
9
PacticeSolution/ListBoxBindingSample/App.xaml
Normal file
9
PacticeSolution/ListBoxBindingSample/App.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="ListBoxBindingSample.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:ListBoxBindingSample"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
17
PacticeSolution/ListBoxBindingSample/App.xaml.cs
Normal file
17
PacticeSolution/ListBoxBindingSample/App.xaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace ListBoxBindingSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
10
PacticeSolution/ListBoxBindingSample/AssemblyInfo.cs
Normal file
10
PacticeSolution/ListBoxBindingSample/AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
53
PacticeSolution/ListBoxBindingSample/MainWindow.xaml
Normal file
53
PacticeSolution/ListBoxBindingSample/MainWindow.xaml
Normal file
@@ -0,0 +1,53 @@
|
||||
<Window x:Class="ListBoxBindingSample.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:ListBoxBindingSample"
|
||||
xmlns:model="clr-namespace:ListBoxBindingSample.Model"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="400" Width="700">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="250"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<ListBox x:Name="lstMain" Grid.Column="0" HorizontalAlignment="Stretch"
|
||||
SelectionMode="Multiple"
|
||||
SelectionChanged="lstMain_SelectionChanged">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="60"/>
|
||||
<ColumnDefinition Width="20"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Grid.Column="0" Margin="2" Text="{Binding Name}"/>
|
||||
<TextBlock Grid.Column="1" Margin="2" Text="{Binding Age}"/>
|
||||
<ProgressBar Grid.Column="2" Margin="5" Width="150"
|
||||
Minimum="0" Maximum="100"
|
||||
Value="{Binding Point}"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<StackPanel Grid.Column="1">
|
||||
<Label Content="<Students management>" Background="LightGray" FontWeight="Bold"/>
|
||||
<Button x:Name="btnSelected" Content="Show selected students" Margin="5" Cursor="Hand"
|
||||
Click="btnSelected_Click"/>
|
||||
<Button x:Name="btnPrevStudent" Content="Prev. student" Margin="5" Cursor="Hand"
|
||||
Click="btnPrevStudent_Click"/>
|
||||
<Button x:Name="btnNextStudent" Content="Next student" Margin="5" Cursor="Hand"
|
||||
Click="btnNextStudent_Click"/>
|
||||
<Button x:Name="btnAll" Content="Show all students" Margin="5" Cursor="Hand"
|
||||
Click="btnAll_Click"/>
|
||||
</StackPanel>
|
||||
|
||||
<TextBox x:Name="tbxDisplay" Grid.Column="2"/>
|
||||
</Grid>
|
||||
</Window>
|
96
PacticeSolution/ListBoxBindingSample/MainWindow.xaml.cs
Normal file
96
PacticeSolution/ListBoxBindingSample/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using ListBoxBindingSample.Model;
|
||||
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 ListBoxBindingSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
InitInstance();
|
||||
}
|
||||
|
||||
private void InitInstance()
|
||||
{
|
||||
List<Student> students = new List<Student>();
|
||||
students.Add(new Student() { Name = "Carl", Age = 23, Point = 100 });
|
||||
students.Add(new Student() { Name = "Jackson", Age = 35, Point = 80 });
|
||||
students.Add(new Student() { Name = "Philip", Age = 26, Point = 73 });
|
||||
students.Add(new Student() { Name = "Sarah", Age = 43, Point = 95 });
|
||||
|
||||
lstMain.ItemsSource = students;
|
||||
}
|
||||
|
||||
private void lstMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (lstMain.SelectedItem == null)
|
||||
return;
|
||||
|
||||
tbxDisplay.Text = $"Selected student: {((Student)lstMain.SelectedItem).Name}";
|
||||
}
|
||||
|
||||
private void btnSelected_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (lstMain.SelectedItem == null)
|
||||
{
|
||||
MessageBox.Show("Please select a student.");
|
||||
return;
|
||||
}
|
||||
|
||||
tbxDisplay.Text = $"Selected student: {((Student)lstMain.SelectedItem).Name}";
|
||||
}
|
||||
|
||||
private void btnPrevStudent_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (lstMain.SelectedItem == null)
|
||||
return;
|
||||
|
||||
int index = lstMain.SelectedIndex - 1;
|
||||
if (index < 0)
|
||||
index = lstMain.Items.Count - 1;
|
||||
|
||||
lstMain.SelectedIndex = index;
|
||||
}
|
||||
|
||||
private void btnNextStudent_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (lstMain.SelectedItem == null)
|
||||
return;
|
||||
|
||||
int index = lstMain.SelectedIndex + 1;
|
||||
if (index >= lstMain.Items.Count)
|
||||
index = 0;
|
||||
|
||||
lstMain.SelectedIndex = index;
|
||||
}
|
||||
|
||||
private void btnAll_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
lstMain.SelectAll();
|
||||
|
||||
tbxDisplay.Clear();
|
||||
foreach (Student item in lstMain.SelectedItems)
|
||||
{
|
||||
tbxDisplay.Text += $"Selected student: {item.Name}{Environment.NewLine}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
PacticeSolution/ListBoxBindingSample/Model/Student.cs
Normal file
15
PacticeSolution/ListBoxBindingSample/Model/Student.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ListBoxBindingSample.Model
|
||||
{
|
||||
internal class Student
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
public int Point { get; set; }
|
||||
}
|
||||
}
|
@@ -45,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GridSharedSample", "GridSha
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LabelTextBlockTextBoxSample", "LabelTextBlockTextBoxSample\LabelTextBlockTextBoxSample.csproj", "{C1375ECE-FDD2-4C7B-B3F8-1A24F3CF051D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListBoxBindingSample", "ListBoxBindingSample\ListBoxBindingSample.csproj", "{71B7E9C4-C73B-4701-B702-A91AB5630580}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -135,6 +137,10 @@ Global
|
||||
{C1375ECE-FDD2-4C7B-B3F8-1A24F3CF051D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C1375ECE-FDD2-4C7B-B3F8-1A24F3CF051D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C1375ECE-FDD2-4C7B-B3F8-1A24F3CF051D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{71B7E9C4-C73B-4701-B702-A91AB5630580}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{71B7E9C4-C73B-4701-B702-A91AB5630580}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{71B7E9C4-C73B-4701-B702-A91AB5630580}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{71B7E9C4-C73B-4701-B702-A91AB5630580}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Reference in New Issue
Block a user