Vertical scroll in Pivot Page - c#

It's a very stupid question :)
Which property sets vertical scroll?
Developing Windows Phone 7 app, I switched off this property, and I don't understand, how I did it :)
I use ComboBox, and when I create project, I can scroll text in ComboBox, but now I can't

You need to include all the items inside the Scrollviewer, as in pivot page, we can't make the content scroller without scroll viewer.
Here is the sample content.
<ScrollViewer HorizontalAlignment="Left" VerticalAlignment="Top" Height="647" Margin="0" Grid.Row="1">
<Grid x:Name="ContentPanel" Margin="0,0,0,24" Width="480" Height="1470" Grid.Row="1">
<Image x:Name="ImgProcess" Source="/GoogTaxi;component/images/loading2.jpg" Visibility="Collapsed" Stretch="None" Opacity="0.60" VerticalAlignment="Top" />
<TextBlock x:Name="tbkCompanyName" TextWrapping="Wrap" Text="Company Name" Margin="20,20,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtCompanyName" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,47,8,0"/>
<TextBlock x:Name="tbkAddress" TextWrapping="Wrap" Text="Address" Margin="20,120,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAddress" AcceptsReturn="True" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,147,8,336" VerticalScrollBarVisibility="Auto" Height="131"/>
<TextBlock x:Name="tbkEmailId" TextWrapping="Wrap" Text="Email Id" Margin="20,279,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtEmailId" IsEnabled="False" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,306,8,0" d:LayoutOverrides="VerticalAlignment"/>
<TextBlock x:Name="tbkWebsite" TextWrapping="Wrap" Margin="20,379,0,0" VerticalAlignment="Top"><Run Text="Website"/><LineBreak/><Run/></TextBlock>
<TextBox x:Name="txtWebsite" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,406,8,0" d:LayoutOverrides="VerticalAlignment"/>
<TextBlock x:Name="tbkPhone" TextWrapping="Wrap" Text="Phone" Margin="20,479,0,0" VerticalAlignment="Top" d:LayoutOverrides="VerticalAlignment"/>
<TextBox x:Name="txtPhone" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,506,8,0"/>
<TextBlock x:Name="tbkCity" TextWrapping="Wrap" Margin="20,579,0,0" VerticalAlignment="Top" Text="City"/>
<TextBox x:Name="txtCity" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,606,8,0"/>
<TextBlock x:Name="tbkState" TextWrapping="Wrap" Text="State" Margin="20,676,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtState" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,704,8,0"/>
<TextBlock x:Name="tblCountry" TextWrapping="Wrap" Text="Country" Margin="20,780,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtCountry" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,806,8,0"/>
<TextBlock x:Name="tbkCompany_logo" Visibility="Collapsed" TextWrapping="Wrap" Text="Contact Person" Margin="20,0,0,42.602" VerticalAlignment="Top" d:IsHidden="True"/>
<TextBox x:Name="txtCompany_logo" Visibility="Collapsed" TextWrapping="Wrap" Margin="8,0,8,-29.305" VerticalAlignment="Top" d:IsHidden="True"/>
<TextBlock x:Name="tbkContactPerson" TextWrapping="Wrap" Text="Contact Person" Margin="20,879,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtContactPerson" TextWrapping="Wrap" Margin="8,908,8,0" VerticalAlignment="Top"/>
<TextBlock x:Name="tbkContactPhone" TextWrapping="Wrap" Text="Contact Person Phone" Margin="20,982,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtContactPhone" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,1005,8,0"/>
<TextBlock x:Name="tbkContactEmail" TextWrapping="Wrap" Text="Contact person Email ID" Margin="20,1079,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtContactEmail" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,1106,8,0"/>
<TextBlock x:Name="tbkMerchantId" TextWrapping="Wrap" Text="Google Merchant Id" Margin="20,1180,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtMerchantId" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,1209,8,0" IsEnabled="False" />
<TextBlock x:Name="tbkMerchantKey" TextWrapping="Wrap" Text="Google Merchant Key" Margin="20,1282,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtMerchantKey" TextWrapping="Wrap" VerticalAlignment="Top" Margin="8,1310,8,0" IsEnabled="False" />
<Button x:Name="btnsave" Content="Save" Margin="33,1390,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Click="btnsave_Click" />
<Button x:Name="btnCancel" Content="Cancel" Margin="0,1390,33,0" VerticalAlignment="Top" HorizontalAlignment="Right" d:LayoutOverrides="HorizontalAlignment" Click="btnCancel_Click" />
</Grid>
</ScrollViewer>

There is no scroll functionality inside PivotItem. You should use ScrollViewer control to get it by your own.

In a pivot control, if the content is overflowing the vertical page then there should be default "vertical" scrolling available to you.
I had a similar control with "list box" bounded to property. Having the list box should automatically allow you to scroll.
Don't add a scrollviewer over the stack panel as it would make the scrolling enabled for each list item which you don't want.
<controls:PivotItem Header="all authors" Foreground="#FF0C388A">
<Grid>
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding AllAuthorsList}" Foreground="#FF0C388A">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="Auto">
<TextBlock Tap="TextBlockAuthor_Tap" Text="{Binding}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="#FF0C388A"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>

Related

TextBlock when run not displaying entire text

I am building a wpf application, where my text block text is fading out when the application is run.
Code is
<Grid>
<TextBlock Text="RSP Level" Width="78" Height="Auto" HorizontalAlignment="Left" Margin="54.2,3,0,398.6" Grid.Column="3"/>
<TextBlock Text="Hex" Width="60" Height="Auto" HorizontalAlignment="Left" Grid.Column="3" Margin="157.2,36,0,367.6" RenderTransformOrigin="0.993,0.976" Grid.ColumnSpan="2"/>
<TextBlock Text="Hex" Width="29" Height="20" TextWrapping="Wrap" HorizontalAlignment="Left" Grid.Column="3" Margin="31.2,35,0,368.6" RenderTransformOrigin="1.91,0.08" />
<TextBlock Text="Link" Width="29" Height="20" HorizontalAlignment="Left" Grid.Column="3" Margin="31.2,59,0,344.6" RenderTransformOrigin="0.993,0.976"/>
<TextBlock Text="Application" Width="61" Height="20" HorizontalAlignment="Left" Grid.Column="3" Margin="32.2,83,0,320.6" RenderTransformOrigin="0.993,0.976"/>
<TextBlock Text="Physical" Width="66" Height="Auto" HorizontalAlignment="Left" Grid.Column="3" Margin="158.2,61,0,342.6" RenderTransformOrigin="0.993,0.976" Grid.ColumnSpan="2"/>
<TextBlock Text="Link" Width="35" Height="Auto" HorizontalAlignment="Left" Grid.Column="3" Margin="158.2,85,0,318.6" RenderTransformOrigin="0.993,0.976"/>
<TextBlock Text="Application" Width="69" Height="Auto" HorizontalAlignment="Left" Grid.Column="3" Margin="157.2,108,0,295.6" RenderTransformOrigin="0.993,0.976" Grid.ColumnSpan="2"/>
</Grid>
This is not the right way to align contents. You can use StackPanel to place child elements below or beside each other. If you use Grid, define Rows and Columns. Using margins is not a proper way. Read more WPF about layouts here.

Unable to scroll in WPF textblocks using ScrollViewer

I have 2 textblocks on my form. I need to have vertical scrollbars in each of them. Due to some reason, I am not able to get the scrollbars on both. Kindly provide me with some idea.
<Grid>
<Button Content="COMPARE" HorizontalAlignment="Left" Margin="216,30,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBox x:Name="TextBox1" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,69,0,0" TextWrapping="Wrap" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBox1_Loaded" />
<ScrollViewer>
<TextBlock x:Name="TextBlock1" HorizontalAlignment="Left" Margin="10,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBlock1_Loaded" />
</ScrollViewer>
<TextBox x:Name="TextBox2" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" Margin="258,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBox2_Loaded"/>
<ScrollViewer>
<TextBlock x:Name="TextBlock2" HorizontalAlignment="Left" Margin="258,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBlock2_Loaded_1"/>
</ScrollViewer>
<Button Content="EDIT" HorizontalAlignment="Left" Margin="409,30,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<Button Content="HOME" HorizontalAlignment="Left" Margin="21,26,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.23,0.986" Click="Button_Click_2"/>
</Grid>
Try to check this out:
1. XAML Code:
<ScrollViewer Grid.Column="0" Grid.Row="0">
<TextBlock x:Name="ATextBlock" ></TextBlock></ScrollViewer>
<ScrollViewer Grid.Row="0" Grid.Column="1" >
<TextBlock x:Name="BtTextBlock" ></TextBlock></ScrollViewer>
the problem was; the Height definition on the TextBlock. Just remove it and that is.
regards.,
To show vertical scroll bar on a TextBox when needed you can use this XAML
<TextBox .... VerticalScrollBarVisibility="Auto" />
ScrollBar will apear when text does not fit the original space given to controll.

C# Editing text in binding textblock

I have the following code:
<Image Grid.Row="0" Grid.Column="0" Source="/Images/check.png" Stretch="Fill"
Visibility="{Binding Checked}"/>
<TextBlock Grid.Row="0" Grid.Column="1" Tag="{Binding Uid}" Tap="RemindName_Tap"
HorizontalAlignment="Left" Text="{Binding Title}" TextWrapping="Wrap"
Padding="10,2,0,0" Foreground="White" Height="40" Margin="2,0,0,0" FontSize="24"
Width="365"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Something here" Tag="{Binding Uid}"/>
How to edit text in second textblock?
There has to be "time" in the appropriate format. That I have in isolated storage, but I have to be properly formatted.

WPF Fit window size

I am new into WPF and i am developing a app with different images, textbox, etc. The problem is here on my screen it all looks ok according to the size of the window with the properties ive set on the window loaded:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Width = System.Windows.SystemParameters.PrimaryScreenWidth;
Height = System.Windows.SystemParameters.PrimaryScreenHeight;
}
When using the application on my screen it looks like this:
But when using it on a tablet i cant see the Calendar neither the text that says "Fecha nacimiento". Here is the WPF code i am using:
<Window x:Class="BiometricoRegistro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RegistrarCliente" Height="978" Width="1600" Loaded="Window_Loaded" WindowState="Maximized">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="Imagenes\home_bg.jpg"/>
</Grid.Background>
<TextBox x:Name="txtbRut" HorizontalAlignment="Left" Height="51" TextWrapping="Wrap" BorderBrush="Transparent" VerticalAlignment="Top" Width="379" Margin="30,230,0,0" FontSize="35" FontWeight="Bold" PreviewTextInput="txtbRut_PreviewTextInput">
<TextBox.Background>
<ImageBrush ImageSource="Imagenes\input.png"/>
</TextBox.Background>
</TextBox>
<TextBox x:Name="txtbRutVer" HorizontalAlignment="Left" Height="51" TextWrapping="Wrap" BorderBrush="Transparent" VerticalAlignment="Top" Width="74" Margin="430,230,0,0" FontSize="35" FontWeight="Bold" TextAlignment="Center">
<TextBox.Background>
<ImageBrush ImageSource="Imagenes\input.png"/>
</TextBox.Background>
</TextBox>
<TextBox x:Name="txtbNombre" HorizontalAlignment="Left" Height="51" TextWrapping="Wrap" BorderBrush="Transparent" VerticalAlignment="Top" Width="474" Margin="30,330,0,0" FontSize="35" FontWeight="Bold">
<TextBox.Background>
<ImageBrush ImageSource="Imagenes\input.png"/>
</TextBox.Background>
</TextBox>
<TextBox x:Name="txtbApellido" HorizontalAlignment="Left" Height="51" TextWrapping="Wrap" BorderBrush="Transparent" VerticalAlignment="Top" Width="474" Margin="30,430,0,0" FontSize="35" FontWeight="Bold">
<TextBox.Background>
<ImageBrush ImageSource="Imagenes\input.png"/>
</TextBox.Background>
</TextBox>
<TextBox x:Name="txtbEmail" HorizontalAlignment="Left" Height="51" TextWrapping="Wrap" BorderBrush="Transparent" VerticalAlignment="Top" Width="474" Margin="30,530,0,0" FontSize="35" FontWeight="Bold">
<TextBox.Background>
<ImageBrush ImageSource="Imagenes\input.png"/>
</TextBox.Background>
</TextBox>
<Calendar x:Name="calendar" HorizontalAlignment="Left" Height="170" VerticalAlignment="Top" Width="260" Margin="0,720,0,57"/>
<Button x:Name="btnRegistrar" Content="Registrar" HorizontalAlignment="Center" Height="118" VerticalAlignment="Center" Width="296" Margin="775,0,221,100" Click="btnRegistrar_Click"/>
<ComboBox x:Name="cbPorcentaje" HorizontalAlignment="Left" Height="40" Margin="30,630,0,0" VerticalAlignment="Top" Width="230" HorizontalContentAlignment="Center" FontSize="35">
<ComboBoxItem Content="A" FontSize="35"></ComboBoxItem>
<ComboBoxItem Content="B" FontSize="35"></ComboBoxItem>
<ComboBoxItem Content="C" FontSize="35"></ComboBoxItem>
</ComboBox>
<TextBlock HorizontalAlignment="Left" Height="37" Margin="30,176,0,0" TextWrapping="Wrap" Text="Rut cliente" VerticalAlignment="Top" Width="474" FontFamily="/BiometricoRegistro;component/Fonts/#Alex Brush" FontSize="36" Foreground="White"/>
<TextBlock HorizontalAlignment="Left" Height="37" Margin="30,286,0,0" TextWrapping="Wrap" Text="Nombres cliente" VerticalAlignment="Top" Width="474" FontFamily="/BiometricoRegistro;component/Fonts/#Alex Brush" FontSize="36" Foreground="White"/>
<TextBlock HorizontalAlignment="Left" Height="37" Margin="30,386,0,0" TextWrapping="Wrap" Text="Apellidos cliente" VerticalAlignment="Top" Width="474" FontFamily="/BiometricoRegistro;component/Fonts/#Alex Brush" FontSize="36" Foreground="White"/>
<TextBlock HorizontalAlignment="Left" Height="37" Margin="30,486,0,0" TextWrapping="Wrap" Text="Email cliente" VerticalAlignment="Top" Width="474" FontFamily="/BiometricoRegistro;component/Fonts/#Alex Brush" FontSize="36" Foreground="White"/>
<TextBlock HorizontalAlignment="Left" Height="37" Margin="30,586,0,0" TextWrapping="Wrap" Text="Tipo Descuento" VerticalAlignment="Top" Width="474" FontFamily="/BiometricoRegistro;component/Fonts/#Alex Brush" FontSize="36" Foreground="White"/>
<TextBlock HorizontalAlignment="Left" Height="37" Margin="30,678,0,230" TextWrapping="Wrap" Text="Fecha nacimiento" VerticalAlignment="Top" Width="474" FontFamily="/BiometricoRegistro;component/Fonts/#Alex Brush" FontSize="36" Foreground="White"/>
<TextBlock x:Name="txtResult" HorizontalAlignment="Left" Height="64" Margin="501,0,0,57" TextWrapping="Wrap" Text="" VerticalAlignment="Bottom" Width="823" FontSize="46" Foreground="Red" FontFamily="PMingLiU-ExtB"/>
</Grid>
</Window>
Always use appropriate panels . I would prefer Dock and stackpanel with splitters.
assign System.Windows.Window.SizeToContent to System.Windows.SizeToContent.WidthAndHeight.
Use Resource Dictonary for consistency of margins and other values
I would not prefer to use Absolute positions. decide your margin values depending on your content .

The name 'xxx' does not exist in the current context in WP7

this is my xaml code:
<TextBox x:Name="name_box_det" Text="{Binding Name}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
And this is C# code, to get text value:
var name_text_det = name_box_det.Text;
And I'm getting this exception:
The name 'name_box_det' does not exist in the current context
Xaml code is copied from another xaml, but I tried to write completely new and it does not help. Do you know where is error?
This is complete XAML code:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Margin="0" Grid.Row="1" Source="devdesk.png" Stretch="Fill"/>
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,45,12,12"Orientation="Horizontal">
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Height="27" Margin="0,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record index:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox Text="{Binding Index}" x:Name="index_box_det" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Name="record_name" Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record name:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox x:Name="name_box_det" Text="{Binding Name}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Beneficiary:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox x:Name="beneficiary_box_det" Text="{Binding Beneficiary}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Price:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox x:Name="price_box_det" Height="65" Text="{Binding Price}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Deadline:" Foreground="#FF6C6C6C"/>
<TextBox x:Name="deadline_box_det" Text="{Binding Deadline}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Description:" Foreground="#FF6C6C6C" Height="27" VerticalAlignment="Bottom"/>
<TextBox x:Name="description_box_det" Text="{Binding Description}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667" Height="285" VerticalAlignment="Bottom"/>
<Button Width="375" x:Name="edtbtn" Content="Edit this record" Click="edtbtn_Click" Height="88" Margin="-12,-10,0,0" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" BorderThickness="3" Foreground="#FF40AA2F" BorderBrush="#FF40AA2F" Background="{x:Null}"/>
<Button Width="100" x:Name="dltbtn" Click="dltbtn_Click" Height="88" Margin="-12,-98,0,0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" BorderThickness="3" Foreground="#FF40AA2F" BorderBrush="#FF40AA2F" Background="{x:Null}">
<StackPanel>
<Image Source="delete.png"/>
</StackPanel>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
name_box_det exists within the ItemTemplate of your ListBox. This is a different context to your page and hence the error. Because an instance of this TextBlock will exist for every item in the collection your there's no way to know which one you're referring to in the code behind.
I'm guessing you're doing this in the Delete button click event handler. As you haven't provided a full repro of what you're doing here's an example of how it may be done.
Assuming the UI contains:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" />
<Button Content="delete" Grid.Column="1" Click="DeleteClicked"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the DataContext is something like:
this.DataContext = new[]
{
new SimpleViewModel { Name = "one" },
new SimpleViewModel { Name = "two" },
new SimpleViewModel { Name = "three" },
};
Then the click event handler can look like this:
private void DeleteClicked(object sender, RoutedEventArgs e)
{
MessageBox.Show(
"You're trying to delete " +
((sender as FrameworkElement).DataContext as SimpleViewModel).Name);
}

Categories

Resources