摘要:單擊列表框項目,DataTemplate中的文本框獲得焦點,但未選中列表框項目。WPF - ListBox ContentTemplate文本框MouseDown導致列表框項不被選中
我敢肯定這與事件冒泡有關,但我在這裏錯過了一些東西。
我有列表框。每個ListBoxItem的ContentTemplate都被分配給一個包含簡單文本框的DataTemplate。
此TextBox被設計爲顯示爲假的可編輯標籤。
問題:單擊文本框時,ListBox的selectedItem沒有被更新。該文本框正在吞噬mousedown事件,並且從未通知列表框將更新到新項目。
我覺得我在這裏失去了一些愚蠢的東西。有任何想法嗎?有沒有辦法強制事件冒泡到父級ListView?
我已經嘗試了一切,從製作文本框的背景Null到處理previewmousedown事件並設置e.handled = false ;.
的DataTemplate:
<DataTemplate x:Key="ItemTempl">
<TextBox Height="20" Width="200" Name="tbox" Text="{Binding WordText}" HorizontalAlignment="Stretch">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="{x:Null}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsFocused, ElementName=tbox}" Value="True">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
列表視圖:
<ListView HorizontalAlignment="Stretch" ItemsSource="{Binding Something.Words}" Name="MainListView" SelectedItem="{Binding CurrentItem, Mode=TwoWay}" BorderThickness="0" ItemContainerStyle="{StaticResource ContainerStyle}">
</ListView>
我在您的ListView中看不到'ItemTempl'的用法。你也沒有解釋'ContainerStyle'是什麼。 – Blachshma