XAML
<ListBox ItemsSource="{Binding}" HorizontalContentAlignment="Left" x:Name="lstbxindex" SelectionMode="Extended" Foreground="White" FontSize="20px" Height="241" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555"
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal" Margin="5" >
<Label x:Name="items" Content="{Binding ProjectRow.Name}" Margin="35,0,0,0" MouseDoubleClick="items_MouseDoubleClick" Foreground="White"></Label>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当我双击listboxitems时,标签转为使用此文本框更改listboxitems更改文本框。然后单击输入文本框再次转到标签。如何在列表框中添加动态文本框,它绑定了代码隐藏中的值。
答案 0 :(得分:0)
你想要这样的东西。将两个控件放在datatemplate中,其中一个控件折叠。
然后在代码后面切换可见性属性。两者都绑定到同一属性,因此将保持彼此同步。
XAML
<TextBlock Visibility="Visible" Name="TextBlockName" Text="{Binding ProjectRow.Name}" MouseDown="TextBlockName_OnMouseDown" ToolTip="Double click to edit"/>
<TextBox Visibility="Collapsed" Name="TextBoxName" Text="{Binding ProjectRow.Name, UpdateSourceTrigger=PropertyChanged}"/>
背后的代码
private void TextBlockName_OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
this.TextBlockName.Visibility = Visibility.Collapsed;
this.TextBoxName.Visibility = Visibility.Visible;
}
}
private void TextBoxName_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
this.TextBlockName.Visibility = Visibility.Visible;
this.TextBoxName.Visibility = Visibility.Collapsed;
}
答案 1 :(得分:0)
创建自定义网格并使用它:
public class NGrid : Grid
{
public NGrid()
{
_textBox = new TextBox { Visibility = Visibility.Hidden };
_label = new Label();
_textBox.KeyDown += _textBox_KeyDown;
Children.Add(_textBox);
Children.Add(_label);
}
private void _textBox_KeyDown(object sender, KeyEventArgs e)
{
if (_isEditMode && e.Key == Key.Return)
{
_textBox.Visibility = Visibility.Hidden;
_label.Content = _textBox.Text;
}
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
if (e.ClickCount == 2 && _isEditMode == false)
{
_textBox.Visibility = Visibility.Visible;
Keyboard.Focus(_textBox);
}
base.OnMouseDown(e);
}
bool _isEditMode;
TextBox _textBox;
Label _label;
}
没有测试它,但应该可以工作。