通过单击ListBoxItem将TextBox更改为只读



我在ListBoxItem中有一个已禁用的TextBox,因此我可以将其拖放到ListBox中。

双击后,我希望它是Enabled,这样我就可以编辑Text,当我完成后,我想它再次成为Disabled,以进行拖放。

我在ListBoxItem上有MouseDoubleClick事件,但它不会更改TextBox ReadOnly。有人能告诉我如何做到这一点吗。

此时CCD_ 10返回空。我似乎没有按照我尝试的方式访问它。

XAML

<ListBox Name="Locations"  Cursor="Hand" HorizontalAlignment="Left" Height="351" Margin="10,48,0,0" VerticalAlignment="Top" Width="285" ItemsSource="{Binding Locations}" dd:DragDrop.IsDragSource="True"
     dd:DragDrop.IsDropTarget="True" SelectedItem="{Binding SelectedItem}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
                <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBox Name="textBox" Text="{Binding Path=Name}"  IsHitTestVisible="False" Width="270" Background="Transparent" BorderThickness="0" Margin="2"/>
                </StackPanel>
                </DataTemplate>
       </ListBox.ItemTemplate>
</ListBox>

在视图

private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
     ListBoxItem item = sender as ListBoxItem;
     textBox.IsReadOnly = false;
}

这是因为文本框在可视化树中"不存在",WPF在需要时为每个列表项创建一个,所以你永远不会有对它的引用。可以导航可视化树并获得文本框引用,但我建议不要这样做,而是在项目模型中创建一个属性,比如"EditMode",当你将其设置为true时,文本框将通过IsEnabled属性的绑定启用。

最新更新