0

我有一个DataGrid内一个UserControl。我想DataGridTextColumn绑定到一个DateTime字段,只显示时间。AttendDate当用户输入时间时,日期部分(年、月、日)应取自UserControl.

我的第一个想法是将用户控件的属性绑定到ConverterParameter

<DataGridTextColumn Header="From" 
    Binding="{Binding FromDate, Converter={StaticResource TimeConverter},ConverterParameter={Binding AttendDate,ElementName=UC}}"
/>

ConverterParameter不带约束力。然后我想用一个来做到这一点MultiBinding

<DataGridTextColumn Header="משעה" Binding="{Binding FromDate, Converter={StaticResource TimeConverter}}" />
    <DataGridTextColumn.Binding>
        <MultiBinding Converter="{StaticResource TimeConverter}">
            <Binding Path="FromDate" />
            <Binding Path="AttendDate" ElementName="UC" />
        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

但是IMultiValueConverter.Convert——它采用多个参数——仅在格式化显示时调用。IMultiValueConverter.ConvertBack在编辑调用,只接受一个参数 - 输入的字符串。

我怎样才能做到这一点?

(我没有使用 MVVM;我无法更改。)

4

2 回答 2

0

解决方案的一个想法是拥有另一个属性,其中只有一个合并您想要的信息的 getter。

就像是

property string Time {get {return this.FromDate.toshortdate().tostring() + AttendDate.hour.tostring() + attenddate.minutes.tostring()}; }

代码可能不完全是这样,但是您可以绑定此属性以在应显示的位置显示您想要的信息。

问候,

=============编辑===========

我试过这个,一个非常简单的例子......不知道它是否适合你:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:conv ="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <conv:TimeConverter x:Key="tmeConverter"></conv:TimeConverter>
    </Window.Resources>

    <Grid>
        <DataGrid ItemsSource="{Binding listExample}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="teste" >
                    <DataGridTextColumn.Binding>
                        <MultiBinding Converter="{StaticResource tmeConverter}">
                            <Binding Path="FromDate" />
                            <Binding Path="AttendDate" />
                        </MultiBinding>
                    </DataGridTextColumn.Binding>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

隐藏代码 (.cs)

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<Example2> listExample { get; set; }

        public Example2 Test { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            this.listExample = new List<Example2>();
            //listExample.Add(new Example { IsChecked = false, Test1 = "teste" });
            //listExample.Add(new Example { IsChecked = false, Test1 = "TTTTT!" });

            this.Test = new Example2 { AttendDate = "1ui", FromDate = "ff" };
            this.listExample.Add(this.Test);

            DataContext = this;


        }
    }
}

和 Example2 类:

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

namespace WpfApplication1
{
    public class Example2
    {
        public string FromDate { get; set; }

        public string AttendDate { get; set; }
    }
}

转换器:

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

namespace WpfApplication1
{
    class TimeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values[0].ToString() + values[1].ToString();
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

最终窗口出现 3 列,如果我更改最后两列,则自动编辑第一列。

是这样吗?

于 2013-05-29T16:49:42.547 回答
0

摆脱 DataGridTextColumn 并将 DataGridTemplateColumn 与 CellTemplate 一起使用,其中包含绑定到多重绑定的文本块,以及包含绑定到 FromDate 的 TextBox 的单元格编辑模板,可能通过短日期转换器,具体取决于您打算实现的可用性。

关于可能的解决方案:

XAML

    <StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label>From time</Label>
        <DatePicker SelectedDate="{Binding FromTime}"/>
    </StackPanel>
    <DataGrid ItemsSource="{Binding AllUsers}" AutoGenerateColumns="False">
        <DataGrid.Resources>
            <local:TimeConverter x:Key="TimeConverter"/>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate DataType="local:UserViewModel">
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource TimeConverter}">
                                    <Binding ElementName="root" Path="DataContext.FromTime"/>
                                    <Binding Path="AttendTimeHour"/>
                                    <Binding Path="AttendTimeMinute"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate DataType="local:UserViewModel">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock>
                                <Run>Attend on </Run>
                                <Run Text="{Binding ElementName=root, Path=DataContext.FromTime, StringFormat=d}"/>
                                <Run> at </Run>
                            </TextBlock>
                            <TextBox Text="{Binding AttendTimeHour}"/><TextBlock>:</TextBlock>
                            <TextBox Text="{Binding AttendTimeMinute}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

视图模型(UserViewModel)部分:

    private DateTime _attendTime;

    public DateTime AttendTime
    {
        get { return _attendTime; }
        set
        {
            if (value == _attendTime) return;
            _attendTime = value;
            OnPropertyChanged();
            OnPropertyChanged("AttendTimeHour");
            OnPropertyChanged("AttendTimeMinute");
        }
    }

    public int AttendTimeHour
    {
        get { return attendTimeHour; }
        set
        {
            if (value.Equals(attendTimeHour)) return;
            attendTimeHour = value;
            AttendTime = new DateTime(AttendTime.Year, AttendTime.Month, AttendTime.Day, AttendTimeHour, AttendTime.Minute, AttendTime.Second);
            OnPropertyChanged();
        }
    }

    private int _attendTimeMinute;

    public int AttendTimeMinute
    {
        get { return _attendTimeMinute; }
        set
        {
            if (value == _attendTimeMinute) return;
            _attendTimeMinute = value;
            AttendTime = new DateTime(AttendTime.Year, AttendTime.Month, AttendTime.Day, AttendTime.Hour, AttendTimeMinute, AttendTime.Second);
            OnPropertyChanged();
        }
    }

和转换器

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length < 2) return null;
        if (!(values[0] is DateTime && values[1] is int && values[2] is int)) return null;

        var fromDate = (DateTime) values[0];
        var attendTimeHour = (int) values[1];
        var attendTimeMinutes = (int)values[2];

        var result = new DateTime(fromDate.Year, fromDate.Month, fromDate.Day, attendTimeHour, attendTimeMinutes, 0);
        return result.ToString();
    }
于 2013-05-29T21:18:29.523 回答