Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF转盘/旋转元件_Wpf_Xaml_Carousel - Fatal编程技术网

WPF转盘/旋转元件

WPF转盘/旋转元件,wpf,xaml,carousel,Wpf,Xaml,Carousel,我正在开发一个软件,可以使用相机自动拍照。我希望用户能够设置照片间隔。我希望它看起来像某种旋转木马 它应该是这样的: 当我滑动(或用鼠标拖动)时,它应沿给定方向移动: 选定的项目应始终在中间,如果是最后一个,第一个应该向右旋转(反之亦然)。 我已经添加了基本的滑动/拖动功能,它可以识别何时进行了操作以及方向。Hovewer,我不知道如何移动这些物品 目前,项目定义如下: 我知道一些关于动画的基础知识,但我不知道如何像我在这里需要的那样制作动画。 使用其他控件而不是StackPanel和T

我正在开发一个软件,可以使用相机自动拍照。我希望用户能够设置照片间隔。我希望它看起来像某种旋转木马

它应该是这样的:

当我滑动(或用鼠标拖动)时,它应沿给定方向移动:

选定的项目应始终在中间,如果是最后一个,第一个应该向右旋转(反之亦然)。 我已经添加了基本的滑动/拖动功能,它可以识别何时进行了操作以及方向。Hovewer,我不知道如何移动这些物品

目前,项目定义如下:


我知道一些关于动画的基础知识,但我不知道如何像我在这里需要的那样制作动画。
使用其他控件而不是StackPanel和TextBlock元素是否更好?

我基于您在上面声明的样式,但不包括在代码中。您可以在纯xaml和viewmodel中执行此操作。这将是一个非常简单和粗糙的例子。或者,您可以实现一个自定义控件,它是一个在层次结构中某处具有的类,而不是vs中默认cust控件模板提供的默认控件。我建议您查看一下MVVM模式和Galasoft MVVM Light。在本例中,我排除了,它们很容易安装并使用EventToCommand,您可以直接在vm或custcontrol*中使用它们

资源

<ItemsPanelTemplate x:Key="YourItemsPanelTemplate">
   <VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>

<DataTemplate x:Key="YourDataTemplate">
    <TextBlock Style="{StaticResource CountdownElement}" Text="{Binding .}" x:Name="PART_TextBlock"/>
    <DataTemplate.Triggers>
        <DataTrigger
        Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType= {x:Type ListViewItem}},Path=IsSelected}" Value="True"> 
            <!-- Here I'm just changing your fontsize, do whatever you want here :) -->                                       
            <Setter Property="FontSize" Value="34" TargetName="PART_TextBlock"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

<Style x:Key="YourContainerStyle" TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Bottom" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="10,0,10,0" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <ContentPresenter x:Name="PART_ContentPresenter"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="{TemplateBinding Padding}"
                                Content="{TemplateBinding Content}"
                                ContentTemplate="{StaticResource YourDataTemplate}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="YourListBoxStyle" TargetType="ListBox">
    <Setter Property="ItemContainerStyle" Value="{StaticResource YourContainerStyle}"/>
    <Setter Property="ItemTemplate" Value="{StaticResource YourDataTemplate}"/>
    <Setter Property="ItemsPanel" Value="{StaticResource YourItemsPanelTemplate}"/>
</Style>

这是您的样式,现在对于xaml代码,请注意,我在这里绑定了您的项目,并且正在使用上面描述的样式

XAML

<Grid>
    <ListView Background="{StaticResource  GuiSideBarBackgroundColor}" 
        Style="{StaticResource YourListBoxStyle}"
        ItemsSource="{Binding CountDownElements}"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>
<Grid>

视图模型 记住将其设置为视图的datacontext,或者将代码复制到codebehind

public class YourViewModel : INotifyPropertyChanged
{
    private ObservableCollection<string> countDownElements = new ObservableCollection<string> { "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "10s" };
    private string selectedItem;

    public ObservableCollection<string> CountDownElements
    {
        get { return countDownElements; }
        set
        {
            if (Equals(value, countDownElements)) return;
            countDownElements = value;
            OnPropertyChanged();
        }
    }

    public string SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (value == selectedItem) return;
            selectedItem = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator] // remove if you don't have R#
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public类YourViewModel:INotifyPropertyChanged
{
私有可观测集合倒计时元素=新可观测集合{“1s”、“2s”、“3s”、“4s”、“5s”、“6s”、“7s”、“8s”、“9s”、“10s”};
私有字符串selectedItem;
公共可观测集合倒计时元素
{
获取{return countDownElements;}
设置
{
if(等于(value,countDownElements))返回;
倒计时元素=值;
OnPropertyChanged();
}
}
公共字符串SelectedItem
{
获取{return selectedItem;}
设置
{
if(value==selectedItem)返回;
选择editem=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]//如果没有R,请删除#
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
输出

希望它能帮助你,或者至少能把你踢向正确的方向

干杯


斯蒂安

你查过任何可用的控件了吗?也许可以帮你省点麻烦。是的,我已经查过了。不过我想自己创建它,因为我想在这个过程中学习一些东西。谢谢你斯蒂安:)我实际上考虑过使用ListView,但没有尝试,因为我面临另一个问题:如何使ListView无限(或循环)。我想要实现的是让选中的项目始终显示在中心,并使其周围的其他元素循环显示。如我的两幅图所示,选择“10s”时,“3s”向右移动。我是否需要对集合中的元素重新排序并每次重新绘制ListView,或者有更好的方法吗?@Davidesantis因为您可能需要创建自己的itemspanel,我在第一个Microsoft Surface(大表)上完成了学士学位论文编码。实际上几乎和你的问题一样,但我不能分享这个代码,因为它属于我写论文的公司。基本上,您可以创建一个面板,并在控制模板中的面板上设置ItemsHost=true,但它可能会为你指明正确的方向。您需要实现IScrollInfo接口。@DavideDeSantis np!这有点可笑,因为这实际上是我在论文中的关键控制之一,在我最初的回答之后,我之前不记得/意识到:)当时我正在学习C#和WPF。你很可能会偶尔把头撞到墙上,但至少在今天,触摸是主流,是普通窗户和WPF的一部分。