小编Joe*_*jr2的帖子

使用UniformGrid的列表框 - 未居中的项目

我为ItemsPanelTemplate使用UniformGrid有一个列表框.这是一张照片列表.我希望照片在网格的每个单元格的中心水平居中,但似乎无论我做什么,图像都对齐到每个单元格的左侧.这是我目前的XAML:

<Border BorderThickness="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Right">
    <ListBox Name="PhotosListBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True" HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding Path=photo}" HorizontalAlignment="Center"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Border>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我将DataTemplate中的Image控件设置为Horizo​​ntalAlignment ="Center",我认为会这样做,但它不起作用.

我究竟做错了什么?

wpf xaml uniformgrid

4
推荐指数
1
解决办法
3372
查看次数

在更高的DPI设置下将Screen.PrimaryScreen.WorkingArea转换为WPF尺寸

我在我的WPF应用程序中有以下功能,用于将窗口调整为主屏幕的工作区域(整个屏幕减去任务栏):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    int theHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
    int theWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;

    this.MaxHeight = theHeight;
    this.MinHeight = theHeight;
    this.MaxWidth = theWidth;
    this.MinWidth = theWidth;

    this.Height = theHeight;
    this.Width = theWidth;

    this.Top = 0;
    this.Left = 0;
}
Run Code Online (Sandbox Code Playgroud)

只要机器的DPI设置为100%,这种方法效果很好.但是,如果他们将DPI设置得更高,那么这不起作用,并且窗口会溢出屏幕.我意识到这是因为WPF像素与"真实"屏幕像素不同,并且因为我使用WinForms属性来获取屏幕尺寸.

我不知道WPF等效于Screen.PrimaryScreen.WorkingArea.我可以使用哪些东西,无论DPI设置如何都可以使用?

如果没有,那么我想我需要某种缩放,但我不知道如何确定要缩放多少.

如何修改我的功能以考虑不同的DPI设置?

顺便说一下,如果你想知道为什么我需要使用这个函数而不是最大化窗口,那是因为它是一个无边框窗口(WindowStyle ="None"),如果你最大化这种类型的窗口,它覆盖了任务栏.

.net c# wpf dpi

4
推荐指数
1
解决办法
1万
查看次数

命名空间'System.Windows'中不存在类型或命名空间名称'Window'

我正在尝试为WPF Window类编写扩展方法.我在我的解决方案中的类库项目中这样做,以便我可以在解决方案中的所有项目中使用它.

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static void ResizeToPrimaryScreenWorkingArea(this System.Windows.Window w)
        { 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我收到以下错误:

命名空间'System.Windows'中不存在类型或命名空间名称'Window'

是的,我在我的类库项目中添加了对System.Windows和System.Windows.Forms的引用,它们显示在解决方案资源管理器中项目的引用下.

我究竟做错了什么?

c# wpf extension-methods namespaces

2
推荐指数
1
解决办法
1万
查看次数

Java List.contains 具有双精度的对象

假设我有这门课:

public class Student
{
  long studentId;
  String name;
  double gpa;

  // Assume constructor here...
}
Run Code Online (Sandbox Code Playgroud)

我有一个类似的测试:

List<Student> students = getStudents();
Student expectedStudent = new Student(1234, "Peter Smith", 3.89)
Assert(students.contains(expectedStudent)
Run Code Online (Sandbox Code Playgroud)

现在,如果 getStudents() 方法将 Peter 的 GPA 计算为类似于 3.8899999999994,那么该测试将失败,因为 3.8899999999994 != 3.89。

我知道我可以对单个双精度/浮点值做一个容忍度的断言,但是有没有一种简单的方法可以使这个与“包含”一起工作,这样我就不必单独比较 Student 的每个字段(我将编写许多类似的测试,而我将要测试的实际类将包含更多字段)。

我还需要避免修改有问题的类(即学生)以添加自定义相等逻辑。

此外,在我的实际课程中,将有其他 double 值的嵌套列表需要进行容差测试,如果我必须单独断言每个字段,这将使断言逻辑更加复杂。

理想情况下,我想说“请告诉我此列表是否包含该学生,对于任何浮点/双精度字段,请以 .0001 的容差进行比较”

感谢任何保持这些断言简单的建议。

java floating-point collections unit-testing floating-point-comparison

2
推荐指数
1
解决办法
893
查看次数

Django order_by 两个字段,第一个字段最后为空

我想按两个字段对查询集进行排序,比如说时间戳和名称。我将有许多时间戳为空的记录,但所有记录都有一个名称。我希望所有具有时间戳的记录首先出现,按时间戳排序,然后是所有具有空时间戳的记录,但按名称进行辅助排序。

排序结果示例:

Timestamp   Name
---------   ----------
10:00       John
10:00       Kevin
10:00       Xavier
11:00       Arnold
12:00       Steve
(null)      Abe
(null)      Ben
(null)      Catherine
(null)      Zeke
Run Code Online (Sandbox Code Playgroud)

我知道按单个字段排序时可以将空值放在最后,如下所示:

Person.objects.order_by(F('时间戳').asc(nulls_last=True))

我只是不确定如何在那里获取辅助排序字段(名称)。

python sorting django django-models

2
推荐指数
1
解决办法
795
查看次数