Tôi đã thêm một DataTemplate
để một lớp ListBox
để ràng buộc bộ sưu tập của tôi để:Chọn ListBoxItem nếu TextBox trong ItemTemplate được tập trung
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276"
SelectionChanged="lstEmails_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Visibility="Hidden" Content="{Binding ID}"></Label>
<TextBox Width="200" Text="{Binding EmailAddress}"></TextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
này thực hiện chính xác những gì tôi muốn nó làm. Mặc dù khi tôi nhấp vào số TextBox
, thì ListBox
không tự động đặt ListItem
được liên kết là Selected
. Tôi có thể làm điều này trong mã, nhưng tôi muốn sử dụng điều này như một thành phần (không có bất ngờ ở đó).
Bất kỳ ý tưởng nào về cách đạt được điều này?
Điều đó dường như không hoạt động, nó sẽ không cho phép tôi nhấp vào bất kỳ thứ gì. Tôi đã bỏ lỡ một cái gì đó. Đây là XAML mới của tôi.
<UserControl.Resources>
<!--<TextBox x:Key="TB" x:Name="TextBoxInsideListBoxItemTemplate">
<TextBox.Style>-->
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsHitTestVisible" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}, AncestorLevel=1}}"
Value="True">
<Setter Property="IsHitTestVisible" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
<!--</TextBox.Style>
</TextBox>-->
</UserControl.Resources>
<Grid>
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276" SelectionChanged="lstEmails_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--<Label Visibility="Hidden" Content="{Binding ID}"></Label>-->
<TextBox Width="220" Text="{Binding EmailAddress}" >
</TextBox>
<!--<TextBox Width="220" Text="{Binding EmailAddress}" GotFocus="TextBox_GotFocus"></TextBox>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Width="20" Margin="12,0,0,12" Name="btnAdd" VerticalAlignment="Bottom" Click="btnAdd_Click" Height="23" HorizontalAlignment="Left">+</Button>
<Button Width="20" HorizontalAlignment="Left" Margin="30,0,0,12" Name="btnRemove" VerticalAlignment="Bottom" Click="btnRemove_Click" Height="23">-</Button>
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="btnApply" VerticalAlignment="Bottom" Width="49" Click="btnApply_Click">Apply</Button>
</Grid>
Tôi nghĩ rằng nhấp đúp hai lần là chức năng tốt.
Brilliant! Đã lưu tôi hôm nay! –
Đây là một giải pháp tuyệt vời, ngoại trừ nó không tốt với chế độ mở rộng và nhiều lựa chọn. – xvpower