已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。
2天前关闭。
Improve this question
我有一个方法,用来解析和获取用户在WPF中的textBox中键入的时间,然后它应该将一个字符串(如“12:05”)作为值添加到我选择的项目中。
private void addInterviewTime(object sender, RoutedEventArgs e)
{
ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;
if (sc != null && DateTime.TryParseExact(textBox.Text, "HH: mm", out TimeOnly result))
{
sc.DT = result;
}
}
它给出以下错误:
// and for sc.DT = result;
Error CS0029 Cannot implicitly convert type 'System.TimeOnly' to 'System.DateTime?'
我也试过
if (sc != null && TimeOnly.TryParseExact(textBox.Text, "HH: mm", out TimeOnly result))
{
sc.DT = result;
}
# with
public class ShortlistedClient : Client, INotifyPropertyChanged
{
private TimeOnly? _dt;
public TimeOnly? DT
{
get { return _dt; }
set { _dt = value; NotifyPropertyChanged(); }
}
public bool InterestedinVac { get; private set; }
public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>();
public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc)
{
DT = new TimeOnly();
InterestedinVac = true;
}
而且它仍然
Error CS0029 Cannot implicitly convert type 'System.TimeOnly' to 'System.DateTime?'
之前,我在ShortlistedClient类中设置了如下DT属性:
public class ShortlistedClient : Client, INotifyPropertyChanged
{
private DateTime? _dt;
public DateTime? DT
{
get { return _dt; }
set { _dt = value; NotifyPropertyChanged(); }
}
public bool InterestedinVac { get; private set; }
public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>();
public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc)
{
DT = new DateTime();
InterestedinVac = true;
}
我的标记代码:
<Window x:Class="WpfApp_Employment_Help.Shortlist"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp_Employment_Help"
mc:Ignorable="d"
Title="Shortlist" Height="450" Width="800">
<StackPanel Margin="0,0,0,73">
<DataGrid x:Name="dgr" AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False" Height="154" Width="793">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Email" Binding="{Binding Email}" />
<DataGridTextColumn Header="Phone" Binding="{Binding Phone}"/>
<DataGridTextColumn Header="Location" Binding="{Binding Location}"/>
<DataGridTextColumn Header="Worktype" Binding="{Binding Worktype}"/>
<DataGridTextColumn Header="Qualification" Binding="{Binding Qualification}"/>
<DataGridTextColumn Header="Workexp" Binding="{Binding Worktype}"/>
<DataGridTextColumn Header="Driving licence" Binding="{Binding Drlicence}"/>
<DataGridTextColumn Header="Criminal conviction" Binding="{Binding Crconviction}"/>
<DataGridTextColumn Header="Interested in vacancy" Binding="{Binding InterestedinVac}"/>
<DataGridTextColumn Header="Interview Date" Binding="{Binding DT}"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="Add Interview DT" Width="128" FontWeight="Bold" Height="28" Click="addInterviewDT"/>
<DatePicker x:Name="DatePick" Height="52" Width="200"/>
<TextBox x:Name="textBox" TextWrapping="Wrap" Width="197" Height="45" Text="Enter time
"/>
<Button x:Name="BtnRemoveShlClient" Content="Remove Shortlisted Client" FontWeight="Bold" Height="33" Width="220" Click="RemoveShClient"/>
</StackPanel>
</Window>
我怎么能只有时间来修复这个错误呢?
1条答案
按热度按时间t5zmwmid1#
DateTime.TryParseExact
解析一个string
并产生一个DateTime
。如果需要
TimeOnly
,则应使用TimeOnly.TryParseExact
方法: