Click here to Skip to main content
15,895,667 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 125.5K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
<UserControl x:Class="CBR.Views.ExplorerView"
             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" 
             mc:Ignorable="d"
             xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"
             xmlns:Converters="clr-namespace:CBR.Components.Converters"
             xmlns:Controls="clr-namespace:CBR.Components.Controls"
             xmlns:Models="clr-namespace:CBR.Core.Models;assembly=CBR.Core"
             xmlns:Views="clr-namespace:CBR.Views"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>

        <!-- group expander style -->
        <Style x:Key="ExpanderGroupContainerStyle"  TargetType="Expander">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="Foreground" Value="{DynamicResource DefaultFontBrush}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Expander">
                        <Grid Margin="2" HorizontalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Name="ContentRow" Height="0"/>
                            </Grid.RowDefinitions>
                            
                            <ToggleButton IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                      OverridesDefaultStyle="True">
                                <ToggleButton.Template>
                                    <ControlTemplate TargetType="ToggleButton">
                                        <Grid Background="Transparent" HorizontalAlignment="Stretch">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Path Width="12" Height="12" x:Name="ExpandPath" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1"
                                                  Fill="{DynamicResource DefaultFontBrush}" Data="M 4 0 L 8 4 L 4 8 Z"/>
                                            <ContentPresenter Grid.Column="1" VerticalAlignment="Center" />
                                        </Grid>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsChecked" Value="True">
                                                <Setter Property="LayoutTransform" TargetName="ExpandPath">
                                                    <Setter.Value>
                                                        <RotateTransform Angle="-90"></RotateTransform>
                                                    </Setter.Value>
                                                </Setter>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </ToggleButton.Template>
                                
                                <Border Name="Content" Grid.Row="1" Background="{DynamicResource ButtonBorderBrush}"
                                        BorderBrush="{DynamicResource ButtonBorderBrush}" BorderThickness="1,0,1,1" CornerRadius="5" >
                                    <ContentPresenter Margin="4" ContentSource="Header" RecognizesAccessKey="True" />
                                </Border>    
                            </ToggleButton>
                            
                            <ContentPresenter Grid.Row="1" Margin="12,0,4,4" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="True">
                                <Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content,Path=DesiredHeight}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="AlphaGroupContainerStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Expander Header="{Binding Name}" IsExpanded="True" Style="{StaticResource ExpanderGroupContainerStyle}">
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- TOOLTIP style and associated data template -->
        <Style x:Key="TransparentToolTipStyle" TargetType="{x:Type ToolTip}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToolTip}">
                        <Border >
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="BookToolTipTemplate" DataType="{x:Type Models:Book}">
            <Grid >
                <Grid.Resources>
                    <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockBase">
                        <Setter Property="Foreground" Value="Silver"/>
                    </Style>
                    <Style TargetType="{x:Type TextBlock}" x:Key="{x:Type TextBlock}" BasedOn="{StaticResource TextBlockBase}">
                    </Style>
                    <Style TargetType="{x:Type TextBlock}" x:Key="Data" BasedOn="{StaticResource TextBlockBase}">
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Style>
                </Grid.Resources>
                <Border Padding="2,2,2,2" CornerRadius="8" BorderBrush="#40000000" BorderThickness="2">
                    <Border CornerRadius="6" ClipToBounds="True" Padding="6,6,6,6">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.75,0.319" StartPoint="0.213,0.672">
                                <GradientStop Color="#99000000"/>
                                <GradientStop Color="#EA000000" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=FileName}" FontSize="14" DockPanel.Dock="Left" Style="{StaticResource Data}" />
                            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Path=FilePath}" />
                            <Image Grid.Column="0" Grid.RowSpan="3" Source="{Binding Path=Cover}" Width="70"/>
                            <Grid Grid.Column="1" Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="12" />
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Column="0" Grid.Row="0" Text="File size:" />
                                <TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding Path=Size}"  Style="{StaticResource Data}"/>
                                <TextBlock Grid.Column="0" Grid.Row="1" Text="Page count:" />
                                <TextBlock Grid.Column="2" Grid.Row="1" Text="{Binding Path=PageCount}"  Style="{StaticResource Data}"/>
                            </Grid>
                        </Grid>
                    </Border>
                </Border>
            </Grid>
        </DataTemplate>
        <!-- END TOOLTIP -->

        <!-- CATALOG style and associated datatemplate -->
        <Style x:Key="CatalogCoverStyle" TargetType="{x:Type ListBox}">
            <Setter Property="Background" Value="#FF3D3D3D" />
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        </Style>

        <!-- Style for an individual generic item -->
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}" >
                        <Border CornerRadius="4" SnapsToDevicePixels="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                            Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="{DynamicResource ButtonPressedInnerBackgroundBrush}"/>
                                <Setter Property="BorderBrush" Value="{DynamicResource ButtonPressedOuterBorderBrush}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{DynamicResource ButtonHoverInnerBackgroundBrush}"/>
                                <Setter Property="BorderBrush" Value="{DynamicResource ButtonHoverOuterBorderBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="PublicTemplate" DataType="{x:Type Models:Book }">
            <Grid Margin="4">
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.9*"/>
                    <RowDefinition Height="0.1*"/>
                </Grid.RowDefinitions>
                <!-- Image Template -->
                <Border x:Name="BorderStatus" BorderThickness="6" CornerRadius="5">
                    <Grid>
                        <Image x:Name="CoverImage" Width="70" Source="{Binding Path=Cover}">
                            <Image.ToolTip>
                                <ToolTip Style="{DynamicResource TransparentToolTipStyle}">
                                    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource BookToolTipTemplate}"/>
                                </ToolTip>
                            </Image.ToolTip>
                        </Image>
                        <Image Source="/CBR;Component/Resources/Images/32x32/push_pin.png" Height="30" HorizontalAlignment="Right" VerticalAlignment="Top"
					Visibility="{Binding Path=Bookmark, Converter={x:Static Converters:BookmarkToVisibilityConverter.Instance}}" Grid.RowSpan="2"></Image>
                        <Image Grid.RowSpan="2"  Width="32" Height="32" VerticalAlignment="Bottom" HorizontalAlignment="Right"
                            Source="{Binding Path=Type, Converter={x:Static Converters:BookTypeToImageConverter.Instance}}" />
                    </Grid>
                </Border>
                <Controls:RatingControl Grid.Row="1" HorizontalAlignment="Stretch" Height="15" RatingValue="{Binding Rating}" />
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsSecured}" Value="True">
                    <Setter TargetName="CoverImage" Property="Source" Value="/CBR;Component/Resources/Images/32x32/forbidden.png" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=IsRead}" Value="True">
                    <Setter TargetName="BorderStatus" Property="BorderBrush" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <!--  style for menu items in sort/group dropdown buttons -->
        <Style x:Key="PropertyViewModel">
            <Setter Property="MenuItem.Header" Value="{Binding ToDisplay}"/>
            <Setter Property="MenuItem.Command" Value="{Binding GenericCommand}" />
            <Setter Property="MenuItem.IsCheckable" Value="true" />
        </Style>

        <Style x:Key="GroupMenuItemStyle" BasedOn="{StaticResource PropertyViewModel}" >
            <Setter Property="MenuItem.CommandParameter" Value="group" />
        </Style>

        <Style x:Key="SortMenuItemStyle" BasedOn="{StaticResource PropertyViewModel}" >
            <Setter Property="MenuItem.CommandParameter" Value="sort" />
        </Style>

    </UserControl.Resources>
    
    <Grid>
        <Grid Visibility="{Binding Path=IsExplorerViewThumb, Converter={x:Static Converters:BoolToVisibilityConverter.Instance}, ConverterParameter=false}">
            <ListView 
            HorizontalAlignment="Stretch" Margin="5" Name="CatalogListView" VerticalAlignment="Stretch" 
            ItemsSource="{Binding Books}" SelectionMode="Single" IsSynchronizedWithCurrentItem="True">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="File" DisplayMemberBinding="{Binding Path=FileName}" />
                            <GridViewColumn Header="Size" DisplayMemberBinding="{Binding Path=Size}" />
                            <GridViewColumn Header="Rating" DisplayMemberBinding="{Binding Path=Rating}" />
                            <GridViewColumn Header="Pages" DisplayMemberBinding="{Binding Path=PageCount}" />
                            <GridViewColumn Header="Is Read" DisplayMemberBinding="{Binding Path=IsRead}" />
                            <GridViewColumn Header="Is Secured" DisplayMemberBinding="{Binding Path=IsSecured}" />
                            <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Path=Type}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
        
        <Grid Visibility="{Binding Path=IsExplorerViewThumb, Converter={x:Static Converters:BoolToVisibilityConverter.Instance}, ConverterParameter=true}">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <DockPanel LastChildFill="False">
                <TextBlock VerticalAlignment="Center" Margin="5,0,5,0">Sort content</TextBlock>

                <Fluent:DropDownButton Header="Sort by" ToolTip="Sort by" SizeDefinition="Small"
                               Icon="/CBR;Component/Resources/Images/16x16/sort_alpha.png"
                                       ItemsSource="{Binding SortProperties}" ItemContainerStyle="{StaticResource SortMenuItemStyle}" />
                
                <Fluent:DropDownButton Header="Group by" ToolTip="Group by" SizeDefinition="Small"
                               Icon="/CBR;Component/Resources/Images/16x16/sort_category.png"
                                       ItemsSource="{Binding GroupProperties}" ItemContainerStyle="{StaticResource GroupMenuItemStyle}" />
            </DockPanel>
            <ListBox Grid.Row="1" BorderThickness="0"
                    VirtualizingStackPanel.IsVirtualizing="True"
                    Name="CatalogListBox" 
                    SelectionMode="Single" 
                    ItemsSource="{Binding Books}"
                    ItemTemplate="{StaticResource PublicTemplate}"
                    Style="{StaticResource CatalogCoverStyle}"
                    SelectedIndex="0"
                    IsSynchronizedWithCurrentItem="True"
                    SelectionChanged="CatalogListBox_SelectionChanged">
                <ListBox.ContextMenu>
                    <ContextMenu>
                    <MenuItem Header="Read" Command="{Binding ForwardCommand}" CommandParameter="BookReadCommand"/>
                    <MenuItem Header="Mark as (un)read" Command="{Binding ForwardCommand}" CommandParameter="BookMarkAsReadCommand"/>
                    <Separator></Separator>
                    <MenuItem Header="Goto bookmark" Command="{Binding ForwardCommand}" CommandParameter="GotoBoormarkCommand"/>
                    <MenuItem Header="Clear bookmark" Command="{Binding ForwardCommand}" CommandParameter="ClearBookmarkCommand"/>
                    <Separator></Separator>
                    <MenuItem Header="Protect" Command="{Binding ForwardCommand}" CommandParameter="BookProtectCommand" />
                    <Separator></Separator>
                    <MenuItem Header="Delete" Command="{Binding ForwardCommand}" CommandParameter="BookDeleteCommand" />
                    <Separator></Separator>
                    <MenuItem Header="Properties" Command="{Binding ForwardCommand}" CommandParameter="BookPropertyCommand" />
                    </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>
        </Grid>
    </Grid>
</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 GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions