Question

I want to be able to update the listview when a user clicks on a listview item (row) and clicks a button. Here is my code:

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>

View Model (ScannerViewModel.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));
        }
    }
  }
}

Model class (Scanner.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; }
 }
}

Can someone tell me how to update one of the listview rows and make the progress bar visible if a user clicks on a row in the list view and clicks the Load To Scanner button? Sample button click code would be appreciated.

Was it helpful?

Solution

Try this

1- in the VM create a list to hold the selected items eg ObservableCollection SelectedScanners {get;set;} 2- In your xaml, bind the SelectedItem of the ListView to the above 3- Create ICommand for the buttons

public ICommand UpdateScannersCommand
{
    get
    {
       return new RelayCommand(()=>
{
... update code here
}
}
}

4- Remove the VISIBILITY property and use a boolean instead, (and use a convertor in the xaml to determine the visibility)

5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top