Click here to Skip to main content
15,895,746 members
Articles / Web Development / HTML

My Personal Commander Variant

Rate me:
Please Sign up or sign in to vote.
4.66/5 (13 votes)
5 Oct 2016CPOL14 min read 44.7K   1.2K   47  
A C#/WPF application for displaying folders on a grid and performing combined functions on them.
<UserControl x:Class="Lobster.MPCV.FolderBrowser"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Lobster.MPCV"
             xmlns:vm="clr-namespace:Lobster.MPCV.VM"
             xmlns:mvvm="clr-namespace:Lobster.MVVM;assembly=Lobster.MVVM"
             xmlns:input="clr-namespace:System.Windows.Input;assembly=WindowsBase"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="FolderBrowserResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <HierarchicalDataTemplate DataType="{x:Type vm:UICommandVM}" ItemsSource="{Binding listMenu}">
                <TextBlock Text="{Binding Path=DisplayName}"/>
            </HierarchicalDataTemplate>

        </ResourceDictionary>
    </UserControl.Resources>
    
    
    <HeaderedContentControl 
        Style="{StaticResource FolderBrowserHCCStyle}"
        Header="{Binding DisplayName}"
        Foreground="White">
        <HeaderedContentControl.Background>
            <SolidColorBrush Color="{Binding Path=DisplayNameBgColor}"/>
        </HeaderedContentControl.Background>
        <HeaderedContentControl.Content>
            <DockPanel>
                <DockPanel DockPanel.Dock="Top">
                    <Button   
                        DockPanel.Dock="Left"
                        Command="{Binding Path=NavigateUp}"
                        Margin="3"
                        Content="&lt;"
                        IsTabStop="False"
                        Focusable="False"
                    />
                    <Button 
                        DockPanel.Dock="Left"
                        Command="{Binding Path=NavigateHome}"
                        Margin="0,3,3,3"
                        Content="^"
                        IsTabStop="False"
                        Focusable="False"
                    />
                    <TextBox  
                        mvvm:TextBoxBehaviors.UpdateSourceOnEnter="true"
                        Text="{Binding Path=CurrentPath, Mode=TwoWay}"
                        HorizontalAlignment="Stretch" Margin="0,3,3,3" 
                        Style="{StaticResource FocusedBorder}"
                        Height="28"
                        VerticalContentAlignment="Center"
                      />  
                </DockPanel>

                <!-- TODO: TextSearch.TextPath="^Model.Pathname" -->
                <!-- see http://stackoverflow.com/questions/8327086/wpf-datagrid-double-click-via-commands-inotifypropertychanged -->
                <!-- This is currently not used but would be required to synchronize multiple selections
                    <DataGrid.RowStyle>
                        <Style>
                            <Setter Property="DataGridRow.IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        </Style>
                    </DataGrid.RowStyle>
                -->
                <!-- DataGrid.ContextMenu see http://social.msdn.microsoft.com/Forums/en/wpf/thread/36ce07fc-64de-4dc9-8012-3c4f03605c30 -->
                <!-- see http://stackoverflow.com/questions/3675198/wpf-optical-style-of-dynamically-created-menuitem-separator-in-mvvm -->
                
                <ListView
                    x:Name="TheDataGrid"
                    Margin="3,0,3,3"
                    ItemsSource="{Binding Items}"
                    SelectedItem="{Binding Selection}"
                    Background="White"
                    TextSearch.TextPath="DisplayName"
                    Style="{StaticResource FocusedBorder}"
                    mvvm:ListViewColumnsSortBehavior.EnableGridViewSort="True"
                    >
                    <ListView.Resources>
                        <!-- see http://stackoverflow.com/questions/560581/how-to-autosize-and-right-align-gridviewcolumn-data-in-wpf -->
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                            <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{Binding Path=BackgroundColor}"/>
                                </Setter.Value>
                            </Setter>
                        </Style>
                        <!-- see http://stackoverflow.com/questions/40733/disable-wpf-label-accelerator-key-text-underscore-is-missing -->
                        <Style x:Key="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" TargetType="Label">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Label">
                                        <Border>
                                            <ContentPresenter
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                RecognizesAccessKey="False" />
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListView.Resources>
                    
                    <ListView.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding Execute}"/>
                        <KeyBinding Key="Backspace" Command="{Binding NavigateUp}"/>
                    </ListView.InputBindings>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction Command="{Binding Path=Execute}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <i:Interaction.Behaviors>
                        <mvvm:FileDropBehavior FileDrop="{Binding FileDrop}" />
                        <mvvm:FileDragBehavior FileDrag="{Binding FileDrag}" />
                        <mvvm:ListViewColumnsSourceBehavior ColumnsSource="{Binding CurrentConfiguration.Columns}" />
                        <mvvm:ListViewEnsureVisibleBehavior />
                    </i:Interaction.Behaviors>
                    <!--
                    <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                <GridViewColumn Header="Bookmarks">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
    
                                            <ItemsControl ItemsSource="{Binding Model.Bookmarks}">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <UniformGrid Rows="1" />
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <Border Height="10" BorderThickness="1" BorderBrush="Black" CornerRadius="3">
                                                            <Border.Background>
                                                                <SolidColorBrush Color="{Binding Color}"/>
                                                            </Border.Background>
                                                        </Border>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>

                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding DisplayName}"/>
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                    -->
                    
                    <ListView.ContextMenu>
                        <ContextMenu ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataContext.ContextMenu}">
                            <ContextMenu.ItemContainerStyle>
                                <Style TargetType="MenuItem">
                                    <Setter Property="Command" Value="{Binding Execute}"/>
                                    <!-- <Setter Property="CommandParameter" Value="{Binding SomeProperty}"/> -->
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding DisplayName}" Value="---">
                                            <Setter Property="Template" >
                                                <Setter.Value>
                                                    <ControlTemplate>
                                                        <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}" />
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContextMenu.ItemContainerStyle>
                        </ContextMenu>
                    </ListView.ContextMenu>
                </ListView>
                <!--
                <mvvm:MVVMDataGrid
                    x:Name="TheDataGrid"
                    Margin="3,0,3,3"
                    ItemsSource="{Binding Items}"
                    SelectedItem="{Binding Selection}"
                    ColumnsSource="{Binding CurrentConfiguration.Columns}"
                    AutoGenerateColumns="False"
                    GridLinesVisibility="None"
                    Background="White"
                    EnsureVisible="true"                    
                    >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction Command="{Binding Path=Execute}" />
                        </i:EventTrigger>                        
                    </i:Interaction.Triggers>
                    <i:Interaction.Behaviors>
                        <mvvm:FileDropBehavior FileDrop="{Binding FileDrop}" />
                        <mvvm:FileDragBehavior FileDrag="{Binding FileDrag}" />
                    </i:Interaction.Behaviors>

                    <DataGrid.Resources>                        
                        <Style TargetType="DataGridCell">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                                        <Grid Background="{TemplateBinding Background}">
                                            <ContentPresenter VerticalAlignment="Center" />
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGrid.Resources>                    
                    <DataGrid.ContextMenu>
                        <ContextMenu ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataContext.ContextMenu}">
                            <ContextMenu.ItemContainerStyle>
                                <Style TargetType="MenuItem">
                                    <Setter Property="Command" Value="{Binding Execute}"/>
                                    <Setter Property="CommandParameter" Value="{Binding SomeProperty}"/>                                    
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding DisplayName}" Value="xxx">
                                            <Setter Property="Template" >
                                                <Setter.Value>
                                                    <ControlTemplate>
                                                        <Separator Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}" />
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContextMenu.ItemContainerStyle>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                </mvvm:MVVMDataGrid>
                -->
                <!--
                <ListBox 
                    Margin="3,0,3,3"
                    mvvm:ListBoxBehaviors.DoubleClick="{Binding Execute}"
                    mvvm:ListBoxBehaviors.FileDrag="{Binding FileDrag}"
                    mvvm:ListBoxBehaviors.FileDrop="{Binding FileDrop}"
                    ItemsSource="{Binding Path=Items}"
                    SelectedItem="{Binding Path=Selection}"
                    HorizontalAlignment="Stretch"  
                    VerticalAlignment="Stretch"
                    mvvm:ListBoxBehaviors.EnsureVisible="true"
                    >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{Binding Icon}" Width="20"/>
                                <Label Content="{Binding DisplayName}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                -->
            </DockPanel>
        </HeaderedContentControl.Content>
    </HeaderedContentControl>
</UserControl>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions