1

I have a listbox for inventory. When I select an item it shows a couple of controls to edit the volume or amount of items. like this:

<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>

Now I need the PLUS and MINUS buttons to edit the content of the textbox.. how can i do it??

I FOUND THE WAY!! THIS IS WHAT I HAVE:

<Window.Resources>
    <DataTemplate x:Key="dtInventory">
        <Border Name="itemBorder" BorderBrush="#FFEBE476" BorderThickness="2" Padding="10" Margin="2" Background="#FF5AB11D">
            <StackPanel x:Name="sp1">
                <StackPanel Orientation="Horizontal" x:Name="spsp1">
                    <StackPanel Width="60">
                        <TextBlock Text="DME Item: "/>
                    </StackPanel>
                    <StackPanel Width="205" x:Name="spsp2">
                        <TextBlock Text="{Binding Path=Name}"/>
                    </StackPanel>
                    <StackPanel Width="60" x:Name="spsp3">
                        <TextBlock Text="Volume: "/>
                    </StackPanel>
                    <StackPanel Width="30" x:Name="spsp4">
                        <TextBlock Text="{Binding Path=Volume}"/>
                    </StackPanel>
                </StackPanel>
                <StackPanel x:Name="sp2" HorizontalAlignment="Right" Orientation="Horizontal" >
                    <Label Content="Edit Volume:" x:Name="l1"/>
                    <myext:IntegerUpDown x:Name="udVolume" Minimum="0" DefaultValue="0"/>
                    <Button Content="OK!" x:Name="bOk" Click="bOk_Click" />
                    <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>
            </StackPanel>
        </Border>
    </DataTemplate>
</Window.Resources>

Then the Listbox:

<ListBox Height="399" HorizontalAlignment="Left" Margin="462,61,0,0" Name="lInventory" VerticalAlignment="Top" Width="390" ItemsSource="{Binding}" ItemTemplate="{StaticResource dtInventory}">

And the .cs:

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); }
        }

I hope it works for other people!

Max
  • 199
  • 1
  • 3
  • 11

1 Answers1

0

Unfortunately you can't do Mathematic oeprations in xaml out of the box. But you could use a Numeric UpDown control (e.g. from the Extented WPF Toolkit)

Or you make your own UserControl where you have your two buttons and your textbox along with the functionality to count up/down. Then use that control in your markup e.g.

<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" >
                            <Label Content="Edit Volume:"/>
<local:MyNumericUpDownControl/>

EDIT:

Here's a link on how to create your own numericupdown control

Community
  • 1
  • 1
SvenG
  • 5,155
  • 2
  • 27
  • 36
  • Ok, thanks! those are good options.. I think I can use the updown control but, how can I read the content from the updown control? Remember the control is inside a datatemplate and it is only visible when the listboxitem is selected.. I need to update the volume value on my database.. – Max Nov 28 '11 at 18:14
  • Well. I'm using it now.. look: but when I try to access the value from the .cs .. It cant find the control.. What I need is to have access to the control value.. – Max Nov 28 '11 at 18:23