在我的应用程序中,我有一个包含列表框的 DayView 窗口,其中包含一些条目。我已经为这个窗口声明了两个数据模板。DefaultDataTemplate 和 EditableData 模板。我也使用过 DataTemplateSelector。当我单击编辑按钮时,我想编辑我的列表框条目。
xml代码:
<!-- Default DataTemplate -->
<DataTemplate x:Key="DefaultDataTemplate" >
<StackPanel Orientation="Horizontal" Width="596">
<TextBlock Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="145"/>
<TextBlock Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="90"/>
<TextBlock Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="100"/>
<TextBlock Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="60"/>
<TextBlock Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="71"/>
<TextBlock Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="130"/>
</StackPanel>
</DataTemplate>
<!-- Editable DataTemplate -->
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<!--<ComboBox x:Name="ClientComboBox" SelectionChanged="ClientComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>-->
<TextBox Text="{Binding ClientNameBinding,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<xctk:TimePicker Value="{Binding StartTimeBinding, ElementName=MainWin, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
<xctk:TimePicker Value="{Binding StopTimeBinding, ElementName=MainWin, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
<ComboBox x:Name="ProjectComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="130" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>
<!-- DataTemplate Selector -->
<l:DayViewListDataTemplateSelector x:Key="templateSelector"
DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
EditableDataTemplate="{StaticResource EditableDataTemplate}"/>
用于编辑列表框项目的 C# 代码:
private void EditButton_Click(object sender, RoutedEventArgs e)
{
foreach (Harvest_TimeSheetEntry item in listBox1.Items)
{
if (item.isSynced)
{
if (listBox1.ItemTemplate == (DataTemplate)this.FindResource("DefaultDataTemplate"))
{
DataTemplate tmpl = (DataTemplate)this.FindResource("EditableDataTemplate");
listBox1.ItemTemplate = tmpl;
this.EditButton.Content = "Done Editing";
}
else
{
DataTemplate tmpl = (DataTemplate)this.FindResource("DefaultDataTemplate");
listBox1.ItemTemplate = tmpl;
this.EditButton.Content = "Edit";
}
}
else
{
Globals._globalController.harvestManager.postHarvestEntry(item);
System.Windows.MessageBox.Show("Entry posted");
}
}
}
DataTemplateSelector 的 C# 代码:
public class DayViewListDataTemplateSelector : DataTemplateSelector
{
public DataTemplate DefaultDataTemplate { get; set; }
public DataTemplate EditableDataTemplate { get; set; }
public override DataTemplate SelectTemplate(object item,DependencyObject container)
{
Harvest_TimeSheetEntry entryToPost = item as Harvest_TimeSheetEntry;
if (entryToPost.isClientEditable == true)
{
return EditableDataTemplate;
}
else
{
return DefaultDataTemplate;
}
}
}
单击编辑按钮时,我正在尝试将模式从 DefaultDataTemplate 更改为 EditableDataTemplate。出现以下问题: 1.转换为EditableDataTemplate时,Existing text不显示。2. 保存编辑的更改时,TimePicker 保留旧值。3. 调试器显示错误 - ItemTemplate 和 ItemTemplateSelector 均已设置。DataTemplateSelector 被忽略。
我不明白,如何在编辑代码中使用 DataTemplateSelector,如果可能请解释一下。