WPF 在列表框中的可分配文本框

本文关键字:可分配 文本 列表 WPF | 更新日期: 2023-09-27 18:32:36

我是wpf的新手,但即便如此,我认为这是一个微不足道的问题。 所以我有 а 列表框有 4 列:文件名、文本框、复选框和一个按钮。按钮工作正常,但由于某种原因我无法使用文本框 - 无法单击它们或在文本框中书写。这是我的 xaml:

<ListBox Name="lbDocxFiles" HorizontalContentAlignment="Stretch" Margin="10,0" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="lbDocxFiles_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition Width="50" />
                    </Grid.ColumnDefinitions>
                    <CheckBox x:Name="checkBox" Grid.Column="3" Content="" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <TextBox  x:Name="tbNodeID" IsReadOnly="False" AcceptsReturn="True" IsEnabled="True" Focusable="True"  TextWrapping="Wrap" HorizontalAlignment="Right" Height="25" Width="90" Text="" VerticalAlignment="Center" Grid.ColumnSpan="1">
                        <TextBox.Style>
                            <Style TargetType="{x:Type TextBox}">
                                <Style.Triggers>
                                    <Trigger Property="IsFocused" Value="True">
                                        <Setter Property="IsReadOnly" Value="False" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                    <Button Grid.Column="2" Width="70" Height="25" Content="UPLOAD" Click="btnUpload_Click" Background="#FF70ECD5" BorderThickness="0" Foreground="White" />
                    <TextBlock Grid.Column="0" Text="{Binding fileTitle}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

我想我做错了什么,但无法找出为什么 texbox 不可编辑。任何帮助将不胜感激!提前感谢!

WPF 在列表框中的可分配文本框

请参考以下代码。它应该有效。有一些 UI 布局问题。

<ListBox x:Name="lbDocxFiles" HorizontalContentAlignment="Stretch"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="90" />
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition Width="50" />
                    </Grid.ColumnDefinitions>
                    <CheckBox x:Name="checkBox" Grid.Column="3" Content="Tets" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <TextBox  x:Name="tbNodeID" IsReadOnly="False" AcceptsReturn="True" 
                              IsEnabled="True" Focusable="True"  TextWrapping="Wrap" HorizontalAlignment="Right" 
                              Height="25" Width="90" Text="" VerticalAlignment="Center" Grid.Column="1">
                        <TextBox.Style>
                            <Style TargetType="{x:Type TextBox}">
                                <Style.Triggers>
                                    <Trigger Property="IsFocused" Value="True">                                            
                                        <Setter Property="IsReadOnly" Value="False" />
                                    </Trigger>
                                    <Trigger Property="IsFocused" Value="False">                                           
                                        <Setter Property="IsReadOnly" Value="True" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                    <Button Grid.Column="2" Width="70" Height="25" Content="UPLOAD" 
                            Background="#FF70ECD5" BorderThickness="0" Foreground="White" />
                    <TextBlock Grid.Column="0" Text="{Binding MyProperty}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
  public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ObservableCollection<MyModel> lst = new ObservableCollection<MyModel>();
        lst.Add(new MyModel() { MyProperty = "Hi" });
        lbDocxFiles.ItemsSource = lst;
    }
}
class MyModel
{
    private string myVar;
    public string MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }
}

所以我在这里找到了一个有效的解决方案。我们在样式触发器中设置 FocusManager.FocusedElement 属性:

 <Window.Resources>
    <Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <StackPanel FocusManager.FocusedElement="{Binding ElementName=MyTextBox}" Orientation="Horizontal">
                        <Grid Margin="0,2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="30" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding fileTitle}" Grid.Column="0" />
                            <TextBox Name="tbNodeID" Text="{Binding Details}" Height="25" Width="90" HorizontalAlignment="Right"  Grid.Column="2" />
                            <Button Grid.Column="1" Width="70" Height="25" HorizontalAlignment="Right" Content="UPLOAD" Click="btnUpload_Click" Background="#FF70ECD5" BorderThickness="0" Foreground="White" />
                            <CheckBox x:Name="checkBox" Grid.Column="3" Content="" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                        </Grid>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Selector.IsSelected" Value="True">
                            <Setter TargetName="tbNodeID" Property="FocusManager.FocusedElement" Value="{Binding ElementName=tbNodeID}" />
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter TargetName="tbNodeID" Property="FocusManager.FocusedElement" Value="{Binding ElementName=tbNodeID}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox Name="myLB" SelectedIndex="{Binding MySelectedIndex}">
    </ListBox> </Grid> 

这不仅使我的文本框可编辑,而且在单击列表框中的项时也可以将其聚焦。