组合框、数据上下文和数据绑定

发布于 2024-12-05 11:02:49 字数 1193 浏览 0 评论 0原文

只是在尝试使用 itemssource、datacontexts 和绑定来显示某些内容时会感到困惑。

public class Date
{
    public DateTime _WeekDate;
    public ICollectionView _WeekData;
}

public class MainWindowViewModel
{
    public ICollectionView WeekDates { get; private set; }

    public MainWindowViewModel()
    {
        List<Date> _dates = new List<Date>();

        //Code to populate _dates

        WeekDates = CollectionViewSource.GetDefaultView(_dates);
    }
}

public MainWindow()
{
    InitializeComponent();

    _ViewModel = new MainWindowViewModel();
    gMain.DataContext = _ViewModel;
}

正如您所看到的,我有一个名为 Date 的类的集合,其中包含一个 DateTime 和另一个集合。这一切都是在 ViewModel 中创建的,该 ViewModel 作为数据上下文附加到网格。

在这个网格中,我有一个组合框,它需要显示集合中每个日期类中列出的日期时间值。因此,我有很多混乱:

<ComboBox ItemsSource="{Binding Path=PickupDates}" DisplayMemberPath="WeekDate" />

这已经起作用了,但是我还希望将绑定转换器应用于 MemberPath 以更改显示日期的样式。但以这种方式应用它我无法添加转换器。无济于事,我尝试过以下方法:

<ComboBox ItemsSource="{Binding Path=PickupDates}" DisplayMemberPath="{Binding Path=WeekDate", Converter={StaticResource DateFormatter}/>

有什么想法吗?或者有更好的方法来做到这一点?

提前致谢, 苏姆盖伊

Just getting confused when trying to display certain things when utilising itemssource, datacontexts and binding.

public class Date
{
    public DateTime _WeekDate;
    public ICollectionView _WeekData;
}

public class MainWindowViewModel
{
    public ICollectionView WeekDates { get; private set; }

    public MainWindowViewModel()
    {
        List<Date> _dates = new List<Date>();

        //Code to populate _dates

        WeekDates = CollectionViewSource.GetDefaultView(_dates);
    }
}

public MainWindow()
{
    InitializeComponent();

    _ViewModel = new MainWindowViewModel();
    gMain.DataContext = _ViewModel;
}

So as you can see I've got a collection of a class called Date, which has a DateTime and another collection inside it. This is all created in a ViewModel which is attached to a grid as a data context.

Within this grid I have a combobox which needs to display the DateTime value listed in each Date class within the collection. So with much messing about I have this:

<ComboBox ItemsSource="{Binding Path=PickupDates}" DisplayMemberPath="WeekDate" />

This has worked however I also want a binding converter to be applied to the MemberPath to change the style in which the date will be displayed. But applying it this way I can't add the Converter. To no avail, I've tried things such as:

<ComboBox ItemsSource="{Binding Path=PickupDates}" DisplayMemberPath="{Binding Path=WeekDate", Converter={StaticResource DateFormatter}/>

Any ideas? Or any better ways of doing this?

Thanks in advance,
SumGuy

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

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

发布评论

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

评论(2

倒带 2024-12-12 11:02:49

您无法绑定到 DisplayMemberPath,但您可以做的是在绑定对象中提供一个属性,类似于您提供的工作示例中的 WeekDate,它以您希望的格式返回 WeekDate。设置 WeekDate 后,引发新格式属性的 propertychanged 事件。

You can't bind to the DisplayMemberPath but what you can do is provide a property within your bound object, similar to WeekDate in the working example you provided, that returns the WeekDate in the format you wish. When WeekDate is set, raise the propertychanged event for the new formatting property.

垂暮老矣 2024-12-12 11:02:49

其他答案的替代方法是设置 ItemStringFormat 为您想要显示的日期时间格式。

<ComboBox ItemsSource="{Binding Path=PickupDates}" 
          ItemStringFormat="ddd dd-MM-yyyy" 
          DisplayMemberPath="WeekDate" /

An alternative to the other answer is to set the ItemStringFormat to the DateTime format that you want to display.

<ComboBox ItemsSource="{Binding Path=PickupDates}" 
          ItemStringFormat="ddd dd-MM-yyyy" 
          DisplayMemberPath="WeekDate" /
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文