Wpf 列表框内的文本框selecteditem。。如何修改文本框内容?
我有一个清单箱。当我选择一个项目时,它会显示两个控件来编辑项目的数量。像这样:Wpf 列表框内的文本框selecteditem。。如何修改文本框内容?,wpf,textbox,listbox,datatemplate,Wpf,Textbox,Listbox,Datatemplate,我有一个清单箱。当我选择一个项目时,它会显示两个控件来编辑项目的数量。像这样: <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" > <Label Content="Edit Volume:"/> <Button Click="bPlus_Click2" Content="+" Height="29" Margin="10,0,0,0" Name="bPlus" Widt
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" >
<Label Content="Edit Volume:"/>
<Button Click="bPlus_Click2" Content="+" Height="29" Margin="10,0,0,0" Name="bPlus" Width="29" />
<TextBox FontSize="16" Height="29" HorizontalContentAlignment="Center" IsReadOnly="True" Name="tNum2" Text="0" VerticalContentAlignment="Center" Width="44" />
<Button Click="bMinus_Click2" Content="-" Height="29" Name="bMinus" Width="29" />
<Button Content="OK!"/>
<StackPanel.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="False">
<Setter Property="StackPanel.Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
我希望它对其他人有用 不幸的是,您无法在xaml开箱即用的情况下进行数学运算。 但是您可以使用数字上下控件(例如,从扩展的WPF工具包) 或者,您可以创建自己的用户控件,其中有两个按钮和文本框以及向上/向下计数的功能。 然后在标记中使用该控件,例如
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" >
<Label Content="Edit Volume:"/>
<local:MyNumericUpDownControl/>
编辑:
关于如何创建自己的数字上下控件好的,谢谢!这些都是不错的选择。。我想我可以使用updown控件,但是,如何从updown控件读取内容?请记住,该控件位于datatemplate中,并且仅在选中listboxitem时才可见。。我需要更新数据库中的卷值。我现在正在用它。。看:但是当我尝试从.cs访问值时。。它找不到控件。。我需要的是访问控制值。。
<ListBox Height="399" HorizontalAlignment="Left" Margin="462,61,0,0" Name="lInventory" VerticalAlignment="Top" Width="390" ItemsSource="{Binding}" ItemTemplate="{StaticResource dtInventory}">
ListBoxItem selecteditem = lInventory.ItemContainerGenerator.ContainerFromIndex(int.Parse(lInventory.SelectedIndex.ToString())) as ListBoxItem;
if (selecteditem != null)
{
try
{
DataTemplate dt = selecteditem.ContentTemplate;
Border border = VisualTreeHelper.GetChild(selecteditem, 0) as Border;
ContentPresenter cp = border.Child as ContentPresenter;
StackPanel sp = dt.FindName("sp1", cp) as StackPanel;
IntegerUpDown updown = sp.FindName("udVolume") as IntegerUpDown;
if (updown.Value != 0)
{
Inventory.DMEItems dme = new Inventory.DMEItems();
dme.Volume = int.Parse(updown.Value.ToString());
dme.DMEInventoryItemID = int.Parse(lInventory.SelectedValue.ToString());
dme.UpdateItem();
UpdateInventory();
}
}
catch (Exception ex)
{ System.Windows.MessageBox.Show("ERROR: " + ex.Message, "Edit Volume" ,MessageBoxButton.OK, MessageBoxImage.Error); }
}
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" >
<Label Content="Edit Volume:"/>
<local:MyNumericUpDownControl/>