Wpf 项目模板当前项目

发布于 2024-08-18 20:48:39 字数 1302 浏览 11 评论 0原文

我有一个简单的 ListBox.ItemTemplate ,其中包含绑定到 CSLA 可绑定列表LabelTextBox。当我选择 TextBox 时,CurrentItem 不会改变,只有当我选择 Label 时它才会改变。我有 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>

I have a simple ListBox.ItemTemplate containing a Label and a TextBox bound to a CSLA Bindable List. When I select the TextBox the CurrentItem does not change, it only changes if I select the Label. I have 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

血之狂魔 2024-08-25 20:48:39

尝试将其添加到您的列表框中。每当任何包含的元素(如 TextBox)获得键盘焦点时,它都会选择该项目。类似的方法也可以仅在触发器中使用一个简单的设置器,但这往往会干扰 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>

Try adding this to your ListBox. It selects the item any time any contained element (like TextBox) gets keyboard focus. 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:

         <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>
心房敞 2024-08-25 20:48:39

发生这种情况是因为 TextBox 正在处理 MouseDown 事件。由于它设置为冒泡,因此它不会到达包含 ListBoxItem。解决此问题的最简单方法是仅处理 PreviewMouseDown 中 ListBoxItems 的选择,这将在实际 MouseDown 事件冒泡之前发生并向下传输。

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

在 xaml 文件的隐藏代码中:

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

This is happening because the TextBox is handling the MouseDown event. As it is set to bubble up it will not reach the containing 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.

<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:

private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    var item = (sender as ListBoxItem);
    if (item != null)
        item.IsSelected = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文