通过单击按钮使用视图模型更新列表视图
本文关键字:视图 模型 更新 列表 单击 按钮 | 更新日期: 2023-09-27 17:57:12
我希望能够在用户单击列表视图项(行)并单击按钮时更新列表视图。这是我的代码:
XAML
<Window x:Class="CS3000Config2.Dialogs.Clone"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CS3000Config2"
Title="Clone Scanner Configuration" Height="768" Width="1024" FontSize="16" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Loaded="Window_Loaded">
<Window.Resources>
<local:ScannerViewModel x:Key="ViewModel" />
</Window.Resources>
<Window.Background>
<LinearGradientBrush EndPoint=".5,1" StartPoint=".5,0">
<GradientStop Color="#b8e1fc" Offset="0" />
<GradientStop Color="#a9d2f3" Offset="0.10" />
<GradientStop Color="#90bae4" Offset="0.25" />
<GradientStop Color="#90bcea" Offset="0.37" />
<GradientStop Color="#90bff0" Offset=".50" />
<GradientStop Color="#6ba8e5" Offset=".51" />
<GradientStop Color="#a2daf5" Offset=".83" />
<GradientStop Color="#bdf3fd" Offset="1" />
</LinearGradientBrush>
</Window.Background>
<DockPanel>
<Grid>
<DockPanel Height="650" Margin="50,40,50,0" Width="800" VerticalAlignment="Top" Background="White">
<Grid>
<ToolBar Height="46" Margin="0,6,-11,0" Name="toolBar1" VerticalAlignment="Top" ToolBarTray.IsLocked="True" BorderBrush="Black" BorderThickness=".5" Loaded="toolBar1_Loaded">
<Button ToolBar.OverflowMode="AsNeeded" Click="Button_Click" Name="btnLoadToSelected">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,0,0,0"/>
</Style>
</StackPanel.Resources>
<Image Source="/CS3000Config2;component/Resources/dsave.png" />
<TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0">Load To Selected Scanners</TextBlock>
</StackPanel>
</Button>
<Separator />
<Button ToolBar.OverflowMode="Never" Click="Button_Click_2" Name="btnLoadToAll">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,0,0,0"/>
</Style>
</StackPanel.Resources>
<Image Source="/CS3000Config2;component/Resources/document-save-all2.png" />
<TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0"> Load To All Scanners</TextBlock>
</StackPanel>
</Button>
<Separator />
<Button ToolBar.OverflowMode="Never" Click="Button_Click_1">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,0,0,0"/>
</Style>
</StackPanel.Resources>
<Image Source="/CS3000Config2;component/Resources/refresh.gif" />
<TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0"> Refresh Scanner List</TextBlock>
</StackPanel>
</Button>
<Separator />
</ToolBar>
<ScrollViewer Margin="6.5,58,6,6" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
<Grid DataContext="{Binding Source={StaticResource ViewModel}}" Name="grid1">
<ListView Margin="0,5" Name="listView1" SelectionMode="Multiple" ItemsSource="{Binding Scanners}">
<ListView.View>
<GridView>
<GridViewColumn Header="" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Height="50" Source="/CS3000Config2;component/Resources/cs3000-small.png" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Drive" Width="50" DisplayMemberBinding="{Binding Drive}" />
<GridViewColumn Header="Model" Width="100" DisplayMemberBinding="{Binding Model}" />
<GridViewColumn Header="Serial" Width="200" DisplayMemberBinding="{Binding Serial}" />
<GridViewColumn Header="" Width="325" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<ProgressBar Name="prog1" Visibility="{Binding isVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Maximum="{Binding maxProgress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="{Binding progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="20" Width="200" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</ScrollViewer>
</Grid>
</DockPanel>
</Grid>
</DockPanel>
视图模型(扫描仪视图模型.cs)
public class ScannerViewModel : INotifyPropertyChanged
{
private String[] scanners;
ObservableCollection<Scanner> items = new ObservableCollection<Scanner>();
private ArrayList driveList = new ArrayList();
public ScannerViewModel()
{
GetDrives();
scanners = (String[])driveList.ToArray(typeof(String));
if (scanners.Length <= 0)
{
return;
}
else
{
foreach (String scnr in scanners)
{
Config c = new Config();
c.LoadSysInfo(scnr + "''Parameters''SYSINFO.TXT");
ProgressBar progBar = new ProgressBar();
progBar.Height = 20;
progBar.Width = 150;
items.Add(new Scanner() { Drive = scnr, Model = c.Model, Serial = c.Serial, maxProgress = 10000, progress = 0, isVisible= Visibility.Hidden });
}
}
}
private void GetDrives()
{
driveList.Clear();
foreach (DriveInfo info in DriveInfo.GetDrives())
{
if (info.DriveType == DriveType.Removable)
{
if (Directory.Exists(info.RootDirectory.ToString() + "Parameters"))
{
driveList.Add(info.RootDirectory.ToString());
}
}
}
}
public ObservableCollection<Scanner> Scanners
{
get
{
return items;
}
set
{
items = value;
RaisePropertyChanged("Scanners");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
模型类(扫描仪.cs)
public class Scanner
{
public String Model { get; set; }
public String Serial { get; set; }
public Visibility isVisible { get; set; }
public String Drive { get; set; }
public Int32 progress { get; set; }
public Int32 maxProgress { get; set; }
}
}
有人可以告诉我,如果用户单击列表视图中的行并单击"加载到扫描仪"按钮,如何更新其中一个列表视图行并使进度条可见?示例按钮单击代码将不胜感激。
试试这个
1-在VM中创建一个列表以保存所选项目,例如ObservableCollection SelectedScanners {get;set;}2-在您的 xaml 中,将列表视图的选定项绑定到上述项3-为按钮创建ICommand
public ICommand UpdateScannersCommand
{
get
{
return new RelayCommand(()=>
{
... update code here
}
}
}
4-删除 VISIBILITY 属性并改用布尔值(并使用 xaml 中的转换器来确定可见性)
5