2

在我的 wpf 应用程序中,有一个 View 类,其中有 ListBox。我为 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)
        { }
     }

我的xml代码:

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

4

1 回答 1

0

我不认为 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)
    { }
 }
于 2013-07-01T16:06:23.613 回答