简体   繁体   English

WPF ItemTemplate CurrentItem

[英]Wpf ItemTemplate CurrentItem

I have a simple ListBox.ItemTemplate containing a Label and a TextBox bound to a CSLA Bindable List . 我有一个简单的ListBox.ItemTemplate其中包含一个Label和一个绑定到CSLA Bindable ListTextBox When I select the TextBox the CurrentItem does not change, it only changes if I select the Label . 当我选择TextBoxCurrentItem不会更改,只有当我选择Label ,它才会更改。 I have IsSynchronizedWithCurrentItem='True' . 我有IsSynchronizedWithCurrentItem='True'

<ListBox x:Name="ItemsDataGrid"
         ItemsSource="{Binding Source={StaticResource AuditItems},Path=Items}"
         IsSynchronizedWithCurrentItem="True">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                </Grid.ColumnDefinitions>        
                <Label Grid.Column="0" 
                       Content="{Binding Path=TypeRef}" />                    
                        <TextBox x:Name="TextBoxQty" 
                                 Grid.Column="1" 
                                 Text="{Binding Path=TaliQty}"/>                         
            </Grid>
        </DataTemplate>                                
    </ListBox.ItemTemplate>                        
</ListBox>

Try adding this to your ListBox. 尝试将其添加到您的ListBox。 It selects the item any time any contained element (like TextBox) gets keyboard focus. 每当包含的任何元素(例如TextBox)获得键盘焦点时,它都会选择该项目。 A similar method could also be used with just a simple setter in the Trigger but that tends to interfere with the CurrentItem setting on the ICollectionView: 类似的方法也可以与Trigger中的一个简单的setter一起使用,但是它会干扰ICollectionView上的CurrentItem设置:

         <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="SetSelected">
                                <Storyboard>
                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                                        <DiscreteBooleanKeyFrame KeyTime="0:00" Value="True" />
                                    </BooleanAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <RemoveStoryboard BeginStoryboardName="SetSelected"/>
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>

This is happening because the TextBox is handling the MouseDown event. 发生这种情况是因为TextBox正在处理MouseDown事件。 As it is set to bubble up it will not reach the containing ListBoxItem. 由于设置为冒泡,它将不会到达包含的ListBoxItem。 The simplest way to fix this would be to just handle the selection of the ListBoxItems in the PreviewMouseDown, which will occur and tunnel down before the actual MouseDown event bubbles up. 解决此问题的最简单方法是只处理PreviewMouseDown中的ListBoxItems的选择,该选择将在实际的MouseDown事件冒泡之前发生并向下隧穿。

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <EventSetter Event="PreviewMouseDown"
                     Handler="ListBoxItem_PreviewMouseDown" />
    </Style>
</ListBox.ItemContainerStyle>

And in the Code behind for the xaml file: 在xaml文件后面的代码中:

private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    var item = (sender as ListBoxItem);
    if (item != null)
        item.IsSelected = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM