如何在WPF中创建赛道记分板

如何在WPF中创建赛道记分板,wpf,sorting,mvvm,observablecollection,Wpf,Sorting,Mvvm,Observablecollection,我正在尝试开发一个赛道记分板,如下所示: 问题是我不知道哪种做法是最好的。实际上,我试图创建一个不断更新的可观察集合。问题是,当我试着按照车手的最佳圈数对记分牌进行分类时,车手的位置总是静止的 我使用了一个CollectionViewSource和一个ListBox来按属性“BestLap”对驱动程序进行排序,但似乎只有在我第一次运行该程序时才对驱动程序的位置进行排序,然后才对任何内容进行排序 我还尝试在viewmodel中不断地对ObservableCollection进行排序,使Drive

我正在尝试开发一个赛道记分板,如下所示:

问题是我不知道哪种做法是最好的。实际上,我试图创建一个不断更新的可观察集合。问题是,当我试着按照车手的最佳圈数对记分牌进行分类时,车手的位置总是静止的

我使用了一个CollectionViewSource和一个ListBox来按属性“BestLap”对驱动程序进行排序,但似乎只有在我第一次运行该程序时才对驱动程序的位置进行排序,然后才对任何内容进行排序

我还尝试在viewmodel中不断地对ObservableCollection进行排序,使Driver类具有可比较性,并创建一个新的ObservableCollection,通过bestlap对比较驱动程序进行排序。但它认为这不是一个好的做法

我想找到一个类似的样品,但我还没有找到。请让我知道,如果你有任何建议,如何做到这一点

对不起我的英语


多谢各位

使用驾驶员等的可观察采集(OC)是正确的方法。此外,使用CollectionViewSource(CVS)也是一种好方法。在您的案例中,由此产生的问题是,当源(OC)发生变化时,您的CV才得以实现。这意味着如果添加或删除了驱动程序

您希望在源对象的属性(如“BestLap”)发生更改时收到通知

stackoverflow和其他处理此问题的网站上有几个问题/答案

现在来看一个可能的解决方案(从第二个链接中提取):启用“IsLiveSortingRequested”并添加一个“SortDescription”,其中包含用于排序的属性

     <Window.Resources>
            <CollectionViewSource x:Key="cvsDrivers" Source="{Binding DriversList}" IsLiveSortingRequested="True">
                <CollectionViewSource.LiveSortingProperties>
                    <clr:String>BestLap</clr:String>
                </CollectionViewSource.LiveSortingProperties>
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="BestLap" />
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </Window.Resources>
ViewModel.cs:

public class DriverViewModel
{
    public ObservableCollection<Driver> DriverList { get; set; }

    public DriverViewModel()
    {
        DriverList = new ObservableCollection<Driver>();
    }
} 

您已经在viewmodel中实现了INotifyPropertyChanged接口?…如果可以,请显示一些代码…这正是我想要的。但我不知道“CollectionViewSource.LiveSortingProperties>BestLap”用于什么。没有这些线,一切都很好。
public class DriverViewModel
{
    public ObservableCollection<Driver> DriverList { get; set; }

    public DriverViewModel()
    {
        DriverList = new ObservableCollection<Driver>();
    }
} 
<Window.Resources>
    <CollectionViewSource x:Key="CvsDriver" 
                          Source="{Binding DriverList}" 
                          IsLiveSortingRequested="True">
        <CollectionViewSource.SortDescriptions>
            <componentModel:SortDescription PropertyName="BestLap" Direction="Ascending" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

    <Style x:Key="DriverListBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="2,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <StackPanel Height="Auto" Orientation="Horizontal">
                            <TextBlock TextWrapping="Wrap" Text="{Binding BestLap, StringFormat=\{0:F2\}}"/>
                            <TextBlock TextWrapping="Wrap" Text="{Binding StartNr}" Margin="8,0,0,0"/>
                            <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Margin="8,0,0,0"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource CvsDriver}}" 
             ItemContainerStyle="{DynamicResource DriverListBoxItemContainerStyle}" />
</Grid>
public partial class MainWindow : Window
{
    private readonly DriverViewModel driverViewModel;

    public MainWindow()
    {
        // Timer generating random BestLap double values from 1.0 to 4.0 every 5 seconds
        DispatcherTimer randomlyUpdateDriverBestLapTimer = new DispatcherTimer();
        randomlyUpdateDriverBestLapTimer.Interval = TimeSpan.FromSeconds(5);
        randomlyUpdateDriverBestLapTimer.Tick += RandomlyUpdateDriverBestLapTimerOnTick;

        driverViewModel = new DriverViewModel();

        Driver driver = new Driver { BestLap = 1.2, Name = "Meyer", StartNr = 1 };
        driverViewModel.DriverList.Add(driver);

        driver = new Driver { BestLap = 1.4, Name = "Sand", StartNr = 2 };
        driverViewModel.DriverList.Add(driver);

        driver = new Driver { BestLap = 1.5, Name = "Huntelaar", StartNr = 3 };
        driverViewModel.DriverList.Add(driver);

        this.DataContext = driverViewModel;

        InitializeComponent();

        randomlyUpdateDriverBestLapTimer.Start();
    }

    private void RandomlyUpdateDriverBestLapTimerOnTick(object sender, EventArgs eventArgs)
    {
        // Important to declare Random object not in the loop because it will generate the same random double for each driver
        Random random = new Random();

        foreach (var driver in driverViewModel.DriverList)
        {
            // Random double from 1.0 - 4.0 (Source code from https://stackoverflow.com/questions/1064901/random-number-between-2-double-numbers)
            driver.BestLap = random.NextDouble() * (4.0 - 1.0) + 1.0;
        }
    }