gpt4 book ai didi

c# - 从一个 DataTemplate 切换到另一个

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:08 26 4
gpt4 key购买 nike

在我的 wpf 应用程序中,有一个包含 ListBox 的 View 类。我为 ListBox Item 的双击事件编写了代码。所以当我双击任何列表框项目时,该项目将发布在我的 Harvest 帐户中。事件如下:

private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
ListBoxItem item = (ListBoxItem)sender;
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)item.DataContext;

if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
}
else
{

//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}

}
catch (Exception)
{ }
}

我的 xaml 代码:

<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
</StackPanel>
</DataTemplate>

<!-- Editable DataTemplate -->
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<ComboBox x:Name="ClientComboBox" SelectionChanged="ProjectComboBoxChanged" 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 ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<ComboBox x:Name="ProjectComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="71" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>




<!-- DataTemplate Selector -->

<l:DayViewListDataTemplateSelector x:Key="templateSelector"
DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
EditableDataTemplate="{StaticResource EditableDataTemplate}"/>

我类有一个计时器,它生成带有两个组合框的 EditableDataTemplate。我的问题是,当我在 ComboBoxes 中选择 Client 和 Project 并双击该条目时,它已发布在我的帐户中,但当时我希望它从 editableDataTemplate 转换为 DefaultDataTemplate(即这两个组合框在 DefaultDataTemplate 中应该同样成为文本框)。我应该如何实现这个结果?

最佳答案

我不认为 DataTemplateSelector 提供了一种根据请求更改数据模板的方法,它只是用于为不同类型的数据(而不是数据状态)选择不同的模板。我认为最好的方法可能是向您的数据模型添加一个属性,我们称之为 IsInEditMode。然后,您可以将 TextBlock 和 Combobox 添加到您的数据模板,并根据 IsInEditMode 的值切换它们的可见性。

顺便说一下:如果您在 DoubleClick-eventhandler 中使用 ListBox.SelectedItem 属性,您可以直接访问数据模型元素,而无需先获取 ListBoxItem 然后访问它的数据上下文。

private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
if(listBox1.SelectedItem is Harvest_TimeSheetEntry)
{
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)listBox1.SelectedItem;

if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");

entryToPost.IsInEditMode = true; //set edit mode!
}
}
else
{

//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}

}
catch (Exception)
{ }
}

关于c# - 从一个 DataTemplate 切换到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17402510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com