【问题标题】:Switch from one DataTemplate to other从一个 DataTemplate 切换到另一个
【发布时间】:2013-06-28 11:00:09
【问题描述】:

在我的 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 中同样成为文本框)。我应该如何实现这个结果?

【问题讨论】:

标签: c# wpf double-click listboxitems datatemplate


【解决方案1】:

我不认为 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)
    { }
 }

【讨论】:

  • 我尝试了您的方法,但是当我双击 listBox 项目时,条目将保存在该发件人对象中。而且我们不能将 listBox 条目直接放入任何类对象中。现在工作正常。问题仅在于切换 DataTemplates。
  • 您是否尝试过添加额外的属性以便不必切换模板?请记住使用 INotifyPropertyChanged 或 Dependency Properties 来实现它,否则对其值的更改将不会自动应用于 UI。
猜你喜欢
  • 2020-10-25
  • 2017-05-03
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2013-07-02
  • 2012-02-01
  • 1970-01-01
相关资源
最近更新 更多