“二维”用户控制

发布于 2024-12-28 09:17:58 字数 774 浏览 1 评论 0原文

我正在尝试实现自定义用户控件。

让我们考虑 ViewModel:

public class FileViewModel
{
    public string Name { get; set; }
    public BitmapSource Thumbnail { get; set; }
}

public class DirectoryViewModel
{
    public string Name { get; set; }
    public ObservableCollection<FileViewModel> Files { get; private set; }
    public FileViewModel SelectedFile { get; set; }
}

我想让 UserControl 以这种方式显示此类 ViewModel (两种方式)

1) 二维列表式控件。

2) 二维流式控制。

请注意,每个文件都有其缩略图,并且每个目录都会记住上次查看的文件。如果未选择目录,目录应显示上次查看的文件缩略图(与其本身一样)。

使用左右键和适当的按钮可以更改目录选择。 使用上下键和适当的按钮可以更改文件选择。

有人实现过像这样的二维用户控件吗?

此致, 哔叽。

I am trying to implement a custom user control.

Let's consider the ViewModels:

public class FileViewModel
{
    public string Name { get; set; }
    public BitmapSource Thumbnail { get; set; }
}

public class DirectoryViewModel
{
    public string Name { get; set; }
    public ObservableCollection<FileViewModel> Files { get; private set; }
    public FileViewModel SelectedFile { get; set; }
}

I want to have the UserControl which display such ViewModels in this way (two ways):

1) Two dimensional list-like control.

2) Two dimensional coverflow-like control.

Please note that each file has it's Thumbnail and each directory remembers last-viewed file. Directory should display last-viewed file thumbnail (as its own) if the directory is not selected.

Directories selection is changed by using left-right keys and appropriate buttons.
Files selection is changed by using up-down keys and appropriate buttons.

Have anyone implemented some two-dimensional UserControl like this?

Best regards,
Serge.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2025-01-04 09:17:58

我可以想到两种方法来实现它,但我现在没有编译器来测试这两种方法。

  • 方法一是覆盖 ListBox 之类的模板(因为您想要跟踪 SelectedItem),以便 SelectedItem总是在同一个地方。更改SelectedItem(通过鼠标或箭头键)只会将新项目移动到列表的中心。

    这可用于文件和目录。使目录使用水平版本的模板,并使其 SelectedItemItemTemplate(使用 DataTrigger)包含垂直版本的文件模板。

  • 我能想到的另一种方法是使用 ItemsControls 和显示下一个/前一个 3 个文件/目录的子集合。

    您可以使用 Linq 语句根据初始集合和当前项目获取上一个/下一个集合。例如,MyCollection.Skip(MyCollection.IndexOf(SelectedItem)).Take(3)

    <Grid>
        <RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefintiion Height="Auto" />
            <RowDefinition Height="*" />
        </RowDefinitions>
        <ColumnDefinitions>
            <ColumnDefinition Height="*" />
            <ColumnDefinition Height="Auto" />
            <ColumnDefinition Height="*" />
        </ColumnDefinitions>

        <!-- Previous 3 Files -->
        <ItemsControl Grid.Row="0" Grid.Column="1"
                      ItemsSource="{Binding Previous3Files}"
                      ItemTemplate="{StaticResource FileTemplate}"
                      ItemsPanel="{Binding VerticalStackPanel}" />

        <!-- Next 3 Files -->    
        <ItemsControl Grid.Row="2" Grid.Column="1"
                      ItemsSource="{Binding Next3Files}"
                      ItemTemplate="{StaticResource FileTemplate}"
                      ItemsPanel="{Binding VerticalStackPanel}" />

        <!-- Previous 3 Directories-->
        <ItemsControl Grid.Row="1" Grid.Column="0"
                      ItemsSource="{Binding Previous3Directories}"
                      ItemTemplate="{StaticResource DirectoryTemplate}"
                      ItemsPanel="{Binding HorizontalStackPanel}" />

        <!-- Next3 Directories-->
        <ItemsControl Grid.Row="1" Grid.Column="2"
                      ItemsSource="{Binding Next3Directories}"
                      ItemTemplate="{StaticResource DirectoryTemplate}"
                      ItemsPanel="{Binding HorizontalStackPanel}" />

        <!-- Current Item -->
        <ContentControl Grid.Row="1" Grid.Column="1"
                        Content="{Binding SelectedFile}"
                        ContentTemplate="{Binding FileTemplate}" />
        </ContentControl>
    
    </Grid>

I can think of two ways to approach it, but I don't have a compiler with me right now to test either way out.

  • Method one would be to overwrite the Template of something like a ListBox (since you want to track the SelectedItem) so that the SelectedItem is always in the same spot. Changing the SelectedItem (via mouse or arrow keys) will simply move the new item to the center of the list.

    This can be used for both the Files and Directories. Make the Directories use a Horizontal version of the Template, and have it's ItemTemplate for the SelectedItem (use a DataTrigger) contain a Vertical version of the template for Files.

  • The other way I could think of doing it is with ItemsControls and sub-collection showing the next/previous 3 files/directories.

    You can use Linq statements to get the Previous/Next collections based on the initial collection and current item. For example, MyCollection.Skip(MyCollection.IndexOf(SelectedItem)).Take(3)

    <Grid>
        <RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefintiion Height="Auto" />
            <RowDefinition Height="*" />
        </RowDefinitions>
        <ColumnDefinitions>
            <ColumnDefinition Height="*" />
            <ColumnDefinition Height="Auto" />
            <ColumnDefinition Height="*" />
        </ColumnDefinitions>

        <!-- Previous 3 Files -->
        <ItemsControl Grid.Row="0" Grid.Column="1"
                      ItemsSource="{Binding Previous3Files}"
                      ItemTemplate="{StaticResource FileTemplate}"
                      ItemsPanel="{Binding VerticalStackPanel}" />

        <!-- Next 3 Files -->    
        <ItemsControl Grid.Row="2" Grid.Column="1"
                      ItemsSource="{Binding Next3Files}"
                      ItemTemplate="{StaticResource FileTemplate}"
                      ItemsPanel="{Binding VerticalStackPanel}" />

        <!-- Previous 3 Directories-->
        <ItemsControl Grid.Row="1" Grid.Column="0"
                      ItemsSource="{Binding Previous3Directories}"
                      ItemTemplate="{StaticResource DirectoryTemplate}"
                      ItemsPanel="{Binding HorizontalStackPanel}" />

        <!-- Next3 Directories-->
        <ItemsControl Grid.Row="1" Grid.Column="2"
                      ItemsSource="{Binding Next3Directories}"
                      ItemTemplate="{StaticResource DirectoryTemplate}"
                      ItemsPanel="{Binding HorizontalStackPanel}" />

        <!-- Current Item -->
        <ContentControl Grid.Row="1" Grid.Column="1"
                        Content="{Binding SelectedFile}"
                        ContentTemplate="{Binding FileTemplate}" />
        </ContentControl>
    
    </Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文