ListView显示复选框和下拉框
上代码,此列表显示checkbox、下拉框 和支持内置搜索。列表设置了一些简单的样式。内置搜索功能可以搜索但是不知道怎么得到搜索出来的结果,还需要继续研究。都是网上搜索结合自己需求整理而成,感觉适合新手看 ^ .^
public class VersionField
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public VersionField(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
public VersionField(string version)
{
FromVersion(version);
}
public static string ToString(string version)
{
string y = version.Substring(version.Length - 5, 2);
string z = version.Substring(version.Length - 3);
string x = version.Substring(0, version.Length - z.Length - y .Length);
string ver = "V" + x + "." + y + "." + z;
return ver;
}
public bool FromVersion(string version)
{
if (version == null || version == "" || !version.StartsWith("V"))
{
X = 0;
Y = 0;
Z = 0;
return false;
}
version = version.Substring(1);
int xpos = version.IndexOf(".");
if (xpos < 0)
{
return false;
}
try
{
string xver = version.Substring(0, xpos);
X = int.Parse(xver);
}
catch
{
return false;
}
int ypos = version.IndexOf(".", xpos + 1);
if (ypos < 0)
{
return false;
}
try
{
string yver = version.Substring(xpos + 1, ypos - xpos - 1);
Y = int.Parse(yver);
}
catch
{
return false;
}
int zpos = version.IndexOf(".", ypos + 1);
if (zpos > 0)
{
return false;
}
try
{
string zver = version.Substring(ypos + 1);
Z = int.Parse(zver);
}
catch
{
return false;
}
return true;
}
public static string GetX(string version)
{
if (!ValidVersion(version))
{
return null;
}
int xpos = version.IndexOf(".");
string ver = version.Substring(1, xpos - 1);
return ver;
}
public static bool ValidVersion(string version)
{
if (version == null || version == "" || !version.StartsWith("V"))
{
return false;
}
version = version.Substring(1);
int xpos = version.IndexOf(".");
if (xpos < 0)
{
return false;
}
try
{
string xver = version.Substring(0, xpos);
int x = int.Parse(xver);
}
catch
{
return false;
}
int ypos = version.IndexOf(".", xpos + 1);
if (ypos < 0)
{
return false;
}
try
{
string yver = version.Substring(xpos + 1, ypos - xpos - 1);
int y = int.Parse(yver);
}
catch
{
return false;
}
int zpos = version.IndexOf(".", ypos + 1);
if (zpos > 0)
{
return false;
}
try
{
string zver = version.Substring(ypos + 1);
int z = int.Parse(zver);
}
catch
{
return false;
}
return true;
}
}
public class SoftObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChang
ed(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class SoftwareData : SoftObservableObject
{
public string Name { get; set; }
public ObservableCollection<string> Versions { get; set; }
public string SelectedVer { get; set; }
public Dictionary<string, string> VerPath { get; set; }
public bool IsCheck { get; set; }
public bool IsEnabled { get; set; }
public SoftwareData(string name, List<string> versions, Dictionary<string, string> verPath, bool isChecked, bool isEnabled = true)
{
Name = name;
OrderVerion(versions);
Versions = new ObservableCollection<string>(versions);
if (Versions != null && Versions.Count > 0)
{
SelectedVer = Versions[0];
}
VerPath = verPath;
IsCheck = isChecked;
IsEnabled = isEnabled;
}
public bool IsChecked
{
get
{
return IsCheck;
}
set
{
if (IsCheck != value)
{
IsCheck = value;
RaisePropertyChanged(nameof(IsChecked));
}
}
}
//对V1.01.001进行拆分各个部分进行对比排序
public List<string> OrderVerion(List<string> versions)
{
List<long> newvers = new List<long>();
foreach (string ver in versions)
{
if (!VersionField.ValidVersion(ver))
{
continue;
}
string subver = ver.Substring(1).Replace(".", "");
newvers.Add(long.Parse(subver));
}
Sort(ref newvers);
versions.Clear();
foreach (long dver in newvers)
{
versions.Add(VersionField.ToString(dver.ToString()));
}
return versions;
}
public void Sort(ref List<long> list)
{
int i = 0, j = 1;
long temp;
bool done = false;
while ((j < list.Count) && (!done))
{
done = true;
for (i = 0; i < list.Count - j; ++i)
{
if (list[i] < list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
++j;
}
}
}
public ObservableCollection<SoftwareData> TargetSource { get; set; }
private void OnViewFilter(object sender, FilterEventArgs e)
{
if (!string.IsNullOrEmpty(txtFilter.Text))
{
e.Accepted = (e.Item as SoftwareData).Name.Contains(txtFilter.Text);
}
}
private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
{
(FindResource("ViewSource") as CollectionViewSource).View.Refresh();
}
<!--TargetSource原始数据-->
<Window.Resources>
<CollectionViewSource x:Key="ViewSource" Filter="OnViewFilter" Source="{Binding TargetSource}"/>
</Window.Resources>
<!--设置背景图片-->
<Grid.Background>
<ImageBrush x:Name="listGridBackground" ImageSource="{Binding BG2Path, Mode=TwoWay}" Stretch="UniformToFill"/>
</Grid.Background>
<!--搜索框-->
<TextBox Grid.Row="0" Name="txtFilter" FontSize="20"
VerticalContentAlignment="Center"
TextChanged="txtFilter_TextChanged">
<TextBox.Resources>
<VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="1.0" Stretch="None" AlignmentX="Center" AlignmentY="Center">
<VisualBrush.Visual>
<Grid Background="WhiteSmoke">
<TextBlock FontStyle="Normal" FontSize="16" Opacity="0.8" Text="车型ID搜索栏"/>
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource HelpBrush}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource HelpBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" x:Name="column0"/>
<Grid Grid.Column="1" x:Name="column1"/>
<ListView x:Name="listboxFileChangedRecord" Grid.ColumnSpan="2"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Opacity="0.9" ItemsSource="{Binding Source={StaticResource ViewSource}}"
AlternationCount="5">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="DarkCyan"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="DarkCyan"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn Header="软件ID" Width="{Binding ElementName=column0,Path=ActualWidth}">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox MinHeight="35" Content="软件ID"
Foreground="BlueViolet" FontWeight="Bold" FontSize="24"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
Click="CbListHeaderChecker_Click"/>
</Grid>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="auto"
IsEnabled="{Binding IsEnabled}" FontSize="20"
VerticalAlignment="Center" HorizontalAlignment="Left"
VerticalContentAlignment="Center"
Checked="CbListItemChecker_Checked"
Unchecked="CbListItemChecker_Unchecked">
<ContentPresenter Content="{Binding Name, Mode=TwoWay}" RecognizesAccessKey="False"/>
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="{Binding ElementName=column1,Path=ActualWidth}">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Grid.Column="0" Text="版本"
Foreground="BlueViolet" FontWeight="Bold" FontSize="20"
VerticalAlignment="Bottom" HorizontalAlignment="Left"
TextAlignment="Left"/>
</Grid>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox VerticalAlignment="Center" VerticalContentAlignment="Center"
HorizontalAlignment="Left" Margin="2" MinWidth="120"
FontSize="16" ItemsSource="{Binding Versions}"
SelectedValue="{Binding SelectedVer, Mode=TwoWay}"
SelectedIndex="0"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ListView.AlternationIndex" Value="0">
<Setter Property="Background" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Trigger>
<Trigger Property="ListView.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>