C# 从一个数据模板切换到另一个

C# 从一个数据模板切换到另一个,c#,wpf,double-click,listboxitems,datatemplate,C#,Wpf,Double Click,Listboxitems,Datatemplate,在我的wpf应用程序中,有一个视图类,我在其中创建了ListBox。我为ListBox项目的双击事件编写了代码。因此,当我双击任何列表框项目时,该项目将发布到我的收获帐户中。以下是事件: private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e) { //Submit clicked Entry try { ListBox

在我的wpf应用程序中,有一个视图类,我在其中创建了ListBox。我为ListBox项目的双击事件编写了代码。因此,当我双击任何列表框项目时,该项目将发布到我的收获帐户中。以下是事件:

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。我的问题是,当我在组合框中选择Client和Project并双击条目时,它会发布在我的帐户中,但当时我希望它从editableDataTemplate转换为DefaultDataTemplate(即,这两个组合框在DefaultDataTemplate中也会变成文本框)。如何实现此结果?

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

顺便说一下:如果在双击事件处理程序中使用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)
    { }
 }

您检查过DataTemplateSelector吗@sll,你可能应该在回答中写上。是的。我查过了。我只是想在双击该条目时转换。我尝试了您的方法,但是,当我双击listBox项时,条目保存在该发件人对象中。我们不能将列表框条目直接带入任何类对象。现在工作正常了。问题是仅切换数据模板。您是否尝试添加额外属性以便不必切换模板?记住使用INotifyPropertyChanged或Dependency属性来实现它,否则对其值的更改将不会自动应用于UI。