简体   繁体   English

WPF DataGrid和Avalon TimePicker绑定不起作用

[英]WPF DataGrid and Avalon TimePicker binding is not working

I'm using a the WPF DataGrid from the wpf toolkit and a TimePicker from AvalonControlsLibrary to insert a collection of TimeSpans. 我正在使用wpf工具包中的WPF DataGrid和AvalonControlsLibrary中的TimePicker来插入TimeSpans的集合。 My problem is that bindings are not working inside the DataGrid, and I have no clue of why this isn't working. 我的问题是绑定在DataGrid内部不起作用,而且我不知道为什么它不起作用。

Here is my setup: 这是我的设置:

I have the following XAML: 我有以下XAML:

<Window x:Class="TestMainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpf="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:a="http://schemas.AvalonControls/AvalonControlsLibrary/Controls" SizeToContent="WidthAndHeight" MinHeight="250" MinWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <GroupBox Grid.Row="0">
        <GroupBox.Header>
            Testing it:
        </GroupBox.Header>
        <wpf:DataGrid ItemsSource="{Binding Path=TestSpans}" AutoGenerateColumns="False">
            <wpf:DataGrid.Columns>
                <wpf:DataGridTemplateColumn Header="Start">
                    <wpf:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <a:TimePicker SelectedTime="{Binding Path=Span, Mode=TwoWay}" />
                        </DataTemplate>
                    </wpf:DataGridTemplateColumn.CellEditingTemplate>
                    <wpf:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Span}" />
                        </DataTemplate>
                    </wpf:DataGridTemplateColumn.CellTemplate>
                </wpf:DataGridTemplateColumn>
            </wpf:DataGrid.Columns>
        </wpf:DataGrid>
    </GroupBox>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="1">
        <a:TimePicker SelectedTime="{Binding Path=SelectedTime, Mode=TwoWay}" />
    </StackPanel>
</Grid>

And this is my ViewModel: 这是我的ViewModel:

Imports System.Collections.ObjectModel

Public Class TestMainWindowViewModel

    Private _selectedTime As TimeSpan = DateTime.Now.TimeOfDay
    Public Property SelectedTime() As TimeSpan
        Get
            Return _selectedTime
        End Get
        Set(ByVal value As TimeSpan)
            _selectedTime = value
        End Set
    End Property

    Private _testSpans As ObservableCollection(Of TimeSpanContainer) = New ObservableCollection(Of TimeSpanContainer)
    Public Property TestSpans() As ObservableCollection(Of TimeSpanContainer)
        Get
            Return _testSpans
        End Get
        Set(ByVal value As ObservableCollection(Of TimeSpanContainer))
            _testSpans = value
        End Set
    End Property

    Public Sub New()
        _testSpans.Add(DateTime.Now.TimeOfDay)
        _testSpans.Add(DateTime.Now.TimeOfDay)
        _testSpans.Add(DateTime.Now.TimeOfDay)
    End Sub

End Class

Public Class TimeSpanContainer

    Private _span As TimeSpan
    Public Property Span() As TimeSpan
        Get
            Return _span
        End Get
        Set(ByVal value As TimeSpan)
            _span = value
        End Set
    End Property

    Public Sub New(ByVal t As TimeSpan)
        _span = t
    End Sub

End Class

I'm starting this window in application.xaml.vb like this: 我正在application.xaml.vb中启动此窗口,如下所示:

Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.
    Protected Overrides Sub OnStartup(ByVal e As System.Windows.StartupEventArgs)
        MyBase.OnStartup(e)
        Dim window As TestMainWindow = New TestMainWindow

        window.DataContext = New TestMainWindowViewModel()

        window.Show()

    End Sub

End Class

EDIT 1: I forgot to mention that the binding to SelectedTime TimeSpan works as expected. 编辑1:我忘了提及到SelectedTime TimeSpan的绑定按预期方式工作。 The problem are the bindings inside the DataGrid. 问题出在DataGrid内部。

EDIT 2: Changed example a little bit to show the problem better. 编辑2:稍微更改示例以更好地显示问题。

What do you mean by your bindings aren't working? 您的绑定不起作用是什么意思? Are you getting no value in the timepicker control when you attempt to edit the value? 尝试编辑值时,时间选择控件中没有任何值?

Edit: 编辑:

Ok, I was having this same issue yesterday and I think there is 2 parts to the issue. 好的,我昨天也遇到过同样的问题,我认为问题有两个部分。

  1. If there is no value appearing in the TimePicker control when you switch to edit mode then there is probably a binding issue with the control. 如果在切换到编辑模式时TimePicker控件中没有任何值,则该控件可能存在绑定问题。

  2. The binding to the underlying value I found to be an issue with using the DataGridTemplateColumn. 我发现使用DataGridTemplateColumn绑定到基础值是一个问题。 Basically the grid doesnt handle your databinding back using the same mechanisms of regular bound columns. 基本上,网格不会使用规则绑定列的相同机制来处理数据绑定。 What it means is you need to perform the following binding on your controls within the column: 这意味着您需要对列中的控件执行以下绑定:

    SelectedTime="{Binding Span, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedTime =“ {绑定范围,模式=双向,UpdateSourceTrigger = PropertyChanged}”

This will fix the binding back to the underlying object. 这会将绑定重新固定到基础对象。 However if there is still an issue with the control it may not help you much. 但是,如果控件仍然存在问题,则可能对您没有太大帮助。 Sorry but I haven't used AvalonControlsLibrary so not sure if there is a potential problem there. 抱歉,但我还没有使用过AvalonControlsLibrary,所以不确定那里是否存在潜在问题。 Fixing step 2 solved my issues. 解决步骤2解决了我的问题。

Cheers 干杯

-Leigh -小

I know this is an old question, but I where playing with this exact control having the exact same issue. 我知道这是一个古老的问题,但是我在玩这个完全相同的控件时却遇到了同样的问题。 I looked in the TimePicker class of AvalonControlsLibrary and the constructor looks like this 我查看了AvalonControlsLibrary的TimePicker类,构造函数如下所示

/// <summary>
/// Default constructor
/// </summary>
public TimePicker()
{
    SelectedTime = DateTime.Now.TimeOfDay;   
}

Removing the line setting the SelectedTime restored the databinding behaviour for me making your posted example work as intended. 删除设置SelectedTime的行后,我恢复了数据绑定行为,从而使您发布的示例按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM