How to get the content of component in customized listbox - silverlight

How to get the content of the hyperlink button clicked in the customized list. The code of XAML is posted below.
<ListBox Height="513" HorizontalAlignment="Left" Margin="9,88,0,0" Name="listBox1" VerticalAlignment="Top" Width="436">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<HyperlinkButton Content="{Binding usrname}" Click="eventhandler" Foreground="#FFC8AB14" FontSize="24" />
<TextBlock Text="{Binding msg}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks alot in advance

Your event handler will have a source argument which is a reference to the HyperlinkButton. You can use it as follows:
public void eventhandler(object source, EventArgs e)
{
HyperlinkButton button = source as HyperlinkButton;
var foo = button.Content;
}

Related

WPF add button in combobox

I want to add button in combobox, which contains ItemTemplate. First, that I tried was this:
<ComboBox Name="oilWells_comboBox"
Style="{StaticResource MMComboBox}"
MaxWidth="100"
ItemsSource="{Binding DataContext.OilWellCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
Margin="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Path=IsDisplay}" Checked="FilterDataGrid" Unchecked="FilterDataGrid">
<CheckBox.Content>
<TextBlock MinWidth="100" Text="{Binding Path=Name}" HorizontalAlignment="Center" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</CheckBox.Content>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<Button Content="Clear" Height="20" HorizontalAlignment="Stretch"></Button>
</ComboBox>
But I've got an exception, which said what i cant add items to control, which has ItemTemplate. The second one was this:
<ComboBox Name="oilWells_comboBox"
Style="{StaticResource MMComboBox}"
MaxWidth="100"
ItemsSource="{Binding DataContext.OilWellCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
Margin="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Path=IsDisplay}" Checked="FilterDataGrid" Unchecked="FilterDataGrid">
<CheckBox.Content>
<TextBlock MinWidth="100" Text="{Binding Path=Name}" HorizontalAlignment="Center" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
<Button Content="Clear" Height="20" HorizontalAlignment="Stretch"></Button>
</CheckBox.Content>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But in this case button adds after each checkbox. Have you any ideas, how to do this only once? Thanks you in advance)
Right Click ComboBox in designer surface / or in Document Outline at the left side outside designer surface > Edit Template > Edit copy.
This will generate some Styles under Window.Resources, find ItemsPresenter and wrap it with StackPanel and Button as shown below :
<StackPanel Grid.ColumnSpan="2">
<Button Content="Additional"/>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</StackPanel>
Try this in windows loaded event and it should work.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Button b = new Button();
b.Content = "My Button";
b.Click += new RoutedEventHandler(MyBtn_Click);
oilWells_comboBox.Items.Add(b);
}

How to set the text of textblock present inside a datatemplate of a listbox from c# codebehind?

My listbox XAML looks like this:
<ListBox x:Name="lstbxbProducts" SelectionChanged="lstbxbProducts_SelectionChanged_1" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="80" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Text=" -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="400">
<TextBlock Text="{Binding prddsc}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="180">
<StackPanel>
<TextBlock Text="{Binding um_cod}" Name="txtblkUmcode" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="20"></StackPanel>
<StackPanel Width="20">
<Image Source="/Images/Arrowselection.png" Tapped="Image_Tapped_1" VerticalAlignment="Center" ></Image>
</StackPanel>
</StackPanel>
<StackPanel Width="180">
<TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
</StackPanel>
<StackPanel Width="100">
<TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Right" ></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to set the text of Name="txtblkUmcode" present in my listbox from c# codebhind in this way, but no luck:
TextBlock txtBlk = new TextBlock();
txtBlk = lstbxbProducts.FindName("txtblkUmcode") as TextBlock;
txtBlk .Text = "test";
Please let me know how can I:
set the text of that textblock prsent inside a datatemplate of a my listbox ?
i want to change the text of that textblock of that single listbox item only ?
How can I do this? Please let me know.
You have to avoid this kind of programming practice because if the view changes - for example there comes a modified DataTemplate with other controls displaying that property(Label, TextBox, etc) - can break your application.
Sometimes there is no other solution.
See the answer here: http://msdn.microsoft.com/en-us/library/bb613579.aspx

How to get specific element out of many elements in data template in Windows Phone 7

my listbox:
<ListBox x:Name="listBox" Grid.Row="2" FontSize="26" SelectionChanged="listBox_SelectionChanged" ItemsSource="{Binding SelectedSubGenre.PhotoCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="BurlyWood" BorderThickness="1,1,1,1" Margin="0,0,12,24">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Image Source="{Binding Path}" Height="200" HorizontalAlignment="Left" Width="200" Margin="12,12,12,12"/>
<Button Foreground="yellow" Click="Button_Click" Height="150" Width="150" Content="add"/>
</StackPanel>
<TextBlock Text="{Binding Name}" Margin="12,0,0,0"/>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When the button is clicked I want to do something with the neighboring image of the button, how can I specify this particular image in my code? How can I get the neighboring image based on different buttons clicks
In the button click callback, do the following:
var btn = sender as Button;
var dc = btn.DataContext as YourDataObject;
dc.Path = "/path/to/new/image.jpg";
You might have to use a value converter to change the path from a string into a BitmapImage.

How to get the value from button

I have implemented a listbox with buttons as follows
<Button.Content>
<StackPanel Orientation="Horizontal" Width="440" Height="210">
<Image Source="{Binding Image}" Height="120" Width="120"/>
<StackPanel Orientation="Vertical" Height="200">
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" Text="TITLE" Height="40" FontSize="22"/>
<TextBlock Width="220" Text="{Binding Title}" Height="40" FontSize="22"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" Text="Detail" Height="40" FontSize="22"/>
<TextBlock Width="220" Text="{Binding description}" Height="40" FontSize="22"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" Text="Discount" Height="40" FontSize="22"/>
<TextBlock Width="220" Text="{Binding discount}" Height="40" FontSize="22"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40" >
<TextBlock Width="100" Text="Deal ID" Height="40" FontSize="22"/>
<TextBlock Name="dealID" Width="220" Text="{Binding DID}" Height="40" FontSize="22"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now the problem that i face is when i click the button i want to get the value of dealID thats in the last text block binded to DID. For this i created the event handler for button click as follows
private void Button_Click(object sender, RoutedEventArgs e)
{
Button myButton = sender as Button;
}
Now i dont have any clue what to do next the mybutton.content method is not working for me
I suggest you to use Blend for that, you can declare click event on xaml, its more easy.
u might want to cast the mybutton.content to the template that you have written inside the content i.e. to a stackpanel..

geting information from Treeview with HierarchicalDataTemplate

Good day!
I have such a template:
<common:HierarchicalDataTemplate x:Key="my2ndPlusHierarchicalTemplate" ItemsSource="{Binding Children}">
<StackPanel Margin="0,2,5,2" Orientation="Vertical" Grid.Column="2">
<CheckBox
IsTabStop="False"
IsChecked="False"
Click="ItemCheckbox_Click"
Grid.Column="1"
/>
<TextBlock Text="{Binding Name}" FontSize="16" Foreground="#FF100101" HorizontalAlignment="Left" FontFamily="Verdana" FontWeight="Bold" />
<TextBlock Text="{Binding Description}" FontFamily="Verdana" FontSize="10" HorizontalAlignment="Left" Foreground="#FFA09A9A" FontStyle="Italic" />
<TextBox Width="100" Grid.Column="4" Height="24" LostFocus="TextBox_LostFocus" Name="tbNumber"></TextBox>
</StackPanel>
</common:HierarchicalDataTemplate>
for a Treeview
<controls:TreeView x:Name="tvServices" ItemTemplate="{StaticResource myHierarchicalTemplate}" ItemContainerStyle="{StaticResource expandedTreeViewItemStyle}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="3" BorderBrush="#FFC1BCBC" FontFamily="Verdana" FontSize="14">
</controls:TreeView>
I want to know the Name property of each TextBox in Treeview to make validation of each textbox such as:
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
tbNumber.ClearValidationError();
if ((!tbNumber.Text.IsZakazNumberValid()) && (tbNumber.Text != ""))
{
tbNumber.SetValidation(MyStrings.NumberError);
tbNumber.RaiseValidationError();
isValid = false;
}
else
{
isValid = true;
}
}
and I wnat to see what check boxes were checked
how can I do it?
I'm not sure I'm clear why you need to know the name.
In this case the sender parameter of the LostFocus event is the TextBox in question. Hence you could use:-
TextBox tb = (TextBox)sender;
Now use this tb variable instead of using tbNumber (which doesn't actually exist because its defined in a template).

Resources