列表框中的WPF可用文本框

时间:2016-01-29 16:01:31

标签: c# wpf xaml textbox listbox

我是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不可编辑。任何帮助将不胜感激! 提前致谢!

2 个答案:

答案 0 :(得分:1)

请参阅以下代码。它应该工作。有一些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; }
    }

}

答案 1 :(得分:0)

所以我found a solution here有效。我们在样式触发器中设置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> 

这不仅可以使我的文本框可编辑,而且还可以在单​​击列表框中的项目时将其集中。