How to get an Obejct from WPF ListBox??
-
Hey, I am trying to get an object from my WPF ListBox. I have a WPF usercontrol(ResultsControl) and it uses a Listbox(lbResults) to Display my custom Objects from a Win form. I am making a new WPF usercontrol(ShowInfoUserControl) to display my custom object's information. The showInfoUserControl should only become visible with the objects's information, when I press on an object in the WPF ListBox. I use Hyperlink to tick the showInfoUserControl. I have tried to lbResults.SelectedItem and lbResults.SelectedValue without luck. Could any one help me please? Every advice will be grateful. my method to get my custom objects (code behind for ResultsControl)
public void GetResults(Result result)
{
WPFResult wpfResult = new WPFhResult(result.ObjectId, result.Header, result.Name);searchResultsList.Add(wpfSearchResult); lbResults.ItemsSource = resultsList; }
private void Windows_Loaded(object sender, RoutedEventArgs e)
{
AddHandler(Hyperlink.ClickEvent, (RoutedEventHandler)Infocard_Click);
}method to call the ShowInfoUserControl
private void Infocard_Click(object sender, RoutedEventArgs e)
{
ShowInfoUserControl infocard = new ShowInfoUserControl();
infocard.GetInfocard((Result)lbSearchResults.SelectedValue);
//infocard.GetInfocard((Result)lbSearchResults.SelectedItem);
}These are my code behind in ShowInfoUserControl
public void GetInfocard(Result result)
{
Header.Text = result.Header;
Name.Text = result.Name;
}my Xaml code for ShowInfoUserControl
<UserControl x:Class="WpfResultsControl.ShowInfoUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:svc="clr-namespace:WpfResultsControl"
Height="200" Width="200">
<Grid>
<StackPanel>
<TextBlock x:Name="Header" FontFamily="Tahoma" FontSize="8"></TextBlock>
<TextBlock x:Name="Name" FontFamily="Tahoma" FontSize="4"></TextBlock>
</StackPanel>
</Grid>
</UserControl>Many thanks in advance.
-
Hey, I am trying to get an object from my WPF ListBox. I have a WPF usercontrol(ResultsControl) and it uses a Listbox(lbResults) to Display my custom Objects from a Win form. I am making a new WPF usercontrol(ShowInfoUserControl) to display my custom object's information. The showInfoUserControl should only become visible with the objects's information, when I press on an object in the WPF ListBox. I use Hyperlink to tick the showInfoUserControl. I have tried to lbResults.SelectedItem and lbResults.SelectedValue without luck. Could any one help me please? Every advice will be grateful. my method to get my custom objects (code behind for ResultsControl)
public void GetResults(Result result)
{
WPFResult wpfResult = new WPFhResult(result.ObjectId, result.Header, result.Name);searchResultsList.Add(wpfSearchResult); lbResults.ItemsSource = resultsList; }
private void Windows_Loaded(object sender, RoutedEventArgs e)
{
AddHandler(Hyperlink.ClickEvent, (RoutedEventHandler)Infocard_Click);
}method to call the ShowInfoUserControl
private void Infocard_Click(object sender, RoutedEventArgs e)
{
ShowInfoUserControl infocard = new ShowInfoUserControl();
infocard.GetInfocard((Result)lbSearchResults.SelectedValue);
//infocard.GetInfocard((Result)lbSearchResults.SelectedItem);
}These are my code behind in ShowInfoUserControl
public void GetInfocard(Result result)
{
Header.Text = result.Header;
Name.Text = result.Name;
}my Xaml code for ShowInfoUserControl
<UserControl x:Class="WpfResultsControl.ShowInfoUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:svc="clr-namespace:WpfResultsControl"
Height="200" Width="200">
<Grid>
<StackPanel>
<TextBlock x:Name="Header" FontFamily="Tahoma" FontSize="8"></TextBlock>
<TextBlock x:Name="Name" FontFamily="Tahoma" FontSize="4"></TextBlock>
</StackPanel>
</Grid>
</UserControl>Many thanks in advance.
saku69 wrote:
lbResults.ItemsSource = resultsList;
What is type of resultsList ??
saku69 wrote:
infocard.GetInfocard((Result)lbSearchResults.SelectedValue); //infocard.GetInfocard((Result)lbSearchResults.SelectedItem);
Is resultsList a collection of Result objects ? If yes, then the above statement should work (if something is selected in the listbox i.e.) What does lbSearchResults.SelectedItem show on the click handler ?
-
saku69 wrote:
lbResults.ItemsSource = resultsList;
What is type of resultsList ??
saku69 wrote:
infocard.GetInfocard((Result)lbSearchResults.SelectedValue); //infocard.GetInfocard((Result)lbSearchResults.SelectedItem);
Is resultsList a collection of Result objects ? If yes, then the above statement should work (if something is selected in the listbox i.e.) What does lbSearchResults.SelectedItem show on the click handler ?
Is resultsList a collection of Result objects ? If yes, then the above statement should work (if something is selected in the listbox i.e.) yes, the resultList is a collection that contains custom objecs. What does lbSearchResults.SelectedItem show on the click handler ? It should show a new WPF user control with obeject's info. I use Hyperlink to tick the click handler.
-
Is resultsList a collection of Result objects ? If yes, then the above statement should work (if something is selected in the listbox i.e.) yes, the resultList is a collection that contains custom objecs. What does lbSearchResults.SelectedItem show on the click handler ? It should show a new WPF user control with obeject's info. I use Hyperlink to tick the click handler.
saku69 wrote:
contains custom objecs.
What is the type of the custom object ? Because when you bind to the List that would be the object you would getting as the SelectedItem. You would have to cast the SelectedItem based on that type. e.g.
CustomObject selObject = ((sender as ListBox).SelectedItem) as CustomObject;
saku69 wrote:
What does lbSearchResults.SelectedItem show on the click handler ?
I meant, when you debug it using a breakpoint in the click method what is the value showing for lbSearchResults.SelectedItem ? This should tell you which object is actually being returned as the SelectedItem.
-
saku69 wrote:
contains custom objecs.
What is the type of the custom object ? Because when you bind to the List that would be the object you would getting as the SelectedItem. You would have to cast the SelectedItem based on that type. e.g.
CustomObject selObject = ((sender as ListBox).SelectedItem) as CustomObject;
saku69 wrote:
What does lbSearchResults.SelectedItem show on the click handler ?
I meant, when you debug it using a breakpoint in the click method what is the value showing for lbSearchResults.SelectedItem ? This should tell you which object is actually being returned as the SelectedItem.
-
Thanks this is helpful. My custom object is a type Result, which I get from a Win app. I tried to debug but the value is null. Very wird. I will go through my code and see what is missing.
saku69 wrote:
lbResults.ItemsSource = resultsList;
that statement tells what the list is bound to. So, if you are seeing something in the List means that something is bound to it. The only thing which can make the SelectedItem null now is nothing actually seledted :)
-
saku69 wrote:
lbResults.ItemsSource = resultsList;
that statement tells what the list is bound to. So, if you are seeing something in the List means that something is bound to it. The only thing which can make the SelectedItem null now is nothing actually seledted :)
I have been searching for errors, but it does not seem to be any. My custom objects show in my ListBox, but as soon I click on my objects I get NullReference Exception on my GetInfocard method. I have also tried with event SelectionChanged in Listbox, still without luck. this is my code behind class
namespace ShowResultsControl
private void Infocard_Click(object sender, RoutedEventArgs e)
{
InfocardUserControl infocard = new InfocardUserControl();
Result selObject = ((sender as ListBox).SelectedItem) as Result;
infocard.GetInfocard(selObject);
}and XAML for my Usercontrol(usercontrol1) where my listBox is
<UserControl x:Class="WpfShowResultsControl.GetResultUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:svc="clr-namespace:ShowResultsControl"
Height="300" Width="300" >
<Grid>
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="resultLayout" DataType="WPFResult">
<StackPanel Orientation="Vertical">
<TextBlock Foreground="Blue" FontFamily="Tahoma" >
<Hyperlink Click="Infocard_Click">
<TextBlock Text="{Binding Path=Header}" />
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<ListBox x:Name="lbResults" ItemTemplate="{StaticResource resultLayout}">
</ListBox>
</StackPanel>
</Grid>
</UserControl>Now this is the code for my 2nd usercontrol, it should become visible, when I select an item in Listbox of Usercontrol1 code behind class for my 2nd usercontrol
namespace ShowResultsControl
{
public partial class InfocardUserControl : UserControl
{
public InfocardUserControl()
{
InitializeComponent();
}public void GetInfocard(Result result) { Header.Text = result.Header; Name.Text = result.Name; } }
}
and xaml <UserControl x:Class="Wp
-
I have been searching for errors, but it does not seem to be any. My custom objects show in my ListBox, but as soon I click on my objects I get NullReference Exception on my GetInfocard method. I have also tried with event SelectionChanged in Listbox, still without luck. this is my code behind class
namespace ShowResultsControl
private void Infocard_Click(object sender, RoutedEventArgs e)
{
InfocardUserControl infocard = new InfocardUserControl();
Result selObject = ((sender as ListBox).SelectedItem) as Result;
infocard.GetInfocard(selObject);
}and XAML for my Usercontrol(usercontrol1) where my listBox is
<UserControl x:Class="WpfShowResultsControl.GetResultUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:svc="clr-namespace:ShowResultsControl"
Height="300" Width="300" >
<Grid>
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="resultLayout" DataType="WPFResult">
<StackPanel Orientation="Vertical">
<TextBlock Foreground="Blue" FontFamily="Tahoma" >
<Hyperlink Click="Infocard_Click">
<TextBlock Text="{Binding Path=Header}" />
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<ListBox x:Name="lbResults" ItemTemplate="{StaticResource resultLayout}">
</ListBox>
</StackPanel>
</Grid>
</UserControl>Now this is the code for my 2nd usercontrol, it should become visible, when I select an item in Listbox of Usercontrol1 code behind class for my 2nd usercontrol
namespace ShowResultsControl
{
public partial class InfocardUserControl : UserControl
{
public InfocardUserControl()
{
InitializeComponent();
}public void GetInfocard(Result result) { Header.Text = result.Header; Name.Text = result.Name; } }
}
and xaml <UserControl x:Class="Wp
saku69 wrote:
Result selObject = ((sender as ListBox).SelectedItem) as Result;
Your sender is the Hyperlink control and not the listbox. You are trying to cast a Hyperlink (which is the sender) as a ListBox and hence you are getting a Null object. You are passing the null object to GetInfocard method which is causing the exception. to get the selected item you should be doing
Result selObject = lbResults.SelectedItem as Result
I would suggest you to put a breakpoint in Infocard_Click and step in to find out why something is null. I am sure that it is because of you wrong casting as I have mentioned above.