I bind my combobox from entityframework and when I try to get value from my combobox by selecting value it gives back text like this {NameOfCompany = Name } How can i get only the value Name?
Thats the code in xaml
<ComboBox SelectionChanged="ComboFirma_SelectionChanged" Name="ComboFirma" Margin="109,10,10,0" Height="28" VerticalAlignment="Top">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="txbCombo" Text="{Binding NameOfCompany}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
And I bind it like this.
var query2 = from f in model.Firmas
select new
{
f.NameOfCompany
};
ComboFirma.ItemsSource = query2.ToList();
I tried something like this to gets the selected value but always i get an exception.
var str = (TextBox)ComboFirma.Template.FindName("txbCombo", ComboFirma);
lblCompanyNameShow.Content = str.SelectedText;
ComboBox.SelectedItem will be of a item type. So normally you would need to cast SelectedItem to type of item in your collection. Now, because, in your case it's anonymous type, it's more difficult I think easiest way to get NameOfCompany is to use dynamic
dynamic selectedItem = ComboFirma.SelectedItem;
var name = selectedItem.NameOfCompany;
or you can use SelectedValue/SelectedValuePath
<ComboBox Name="ComboFirma" ... SelectedValuePath="NameOfCompany">
and in code
var name = (string)ComboFirma.SelectedValue;
Try like this
TextBlock tb1 = (TextBlock)ComboFirma.SelectedItem;
lblCompanyNameShow.Content = str.SelectedText;
Related
I have a combobox in my main page.xmal, it is populated from an ObservableCollection and displays a list of Country Names. What I want to happen is, When the user selects the name of the country, I want to pass a code which is assoicated with each country. The Name and the Code are both stored in the ObservableCollection.
I presume I can set a property in the combobox which allow me too, set the selected item equal to the country code. Any help would be great.
<StackPanel Margin="10,100,0,0" Orientation="Vertical" >
<ComboBox Name="countryCombo"
PlaceholderText="Select Country"
ItemsSource="{x:Bind ReadInFile.CountryCodes, Mode=OneWay}"
SelectedIndex="{x:Bind ReadInFile.SelectedIndex}"
DisplayMemberPath="Name"
SelectedValuePath="Code"
MinWidth="250" Margin="5" Opacity="0.65">
</ComboBox>
<TextBox Name="CityTextBox" PlaceholderText="Enter City"/>
</StackPanel>
private async void WeatherCityButton_Click(object sender, RoutedEventArgs e)
{
var city = CityTextBox.Text.ToString();
var country = countryCombo.SelectedValuePath.ToString();
RootObject myCityWeather = await WeatherFacade.GetWeatherCity(country, city);
WeatherResultCityText.Text = myCityWeather.current_observation.display_location.city + " _ " + myCityWeather.current_observation.temp_c.ToString() + "-" + myCityWeather.current_observation.wind_kph;
}
You've already got it set up how you want it - SelectedValuePath does precisely what you describe. Once that's set, accessing the ComboBox's SelectedValue property will provide the code for the selected item.
You need only to change this line:
var country = countryCombo.SelectedValuePath.ToString();
To:
var country = countryCombo.SelectedValue.ToString();
The way you had it simply will return "Code", if I'm not mistaken, as you're retrieving the same value you put in.
I have a listboxitem with a data template that have something like this:
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Age}"/>
and i use this to add values to the bindings.
ListBox1.Items.Add(New User() With {.Name= "John Crooks", .Age = 42})
is it possible to retrieve these data programmatically, for example. i'm trying to get the age of the selected item programmatically. how can i do this?
You could cast the value of the SelectedItem property to your data item type and then access its properties.
In C#:
if (ListBox1.SelectedItem != null)
{
var myItem = (MyItem)ListBox1.SelectedItem;
var name = myItem.Name;
}
That should be something like this in VB:
Dim myItem As MyItem = CType(ListBox1.SelectedItem, MyItem)
Dim name As String = myItem.Name
You need declare in XAML:
xmlns:localData="YourAssembly.dll"
d:DataContext="{d:DesignInstance Type=localData:YourClass}"
I'm using WPF and the timer doesn't allow to use int for interval. Instead, it asks for TimeSpan
timer1.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(comboBox1.SelectedItem));
So I changed my code to this but at runtime it gives me an InvalidCastException, saying that the object cannot be converted from System.Windows.Controls.ComboboxItem to System.IConvertible.
How can I solve this?
You should use this
Convert.ToDouble(comboBox1.SelectedText)
The comboBox1.SelectedItem corresponds to the selected item of the ComboBox control and not with the text of it, which is that you want.
Specifically, the SelectedText property of a CombBox control
Gets or sets the text that is selected in the editable portion of a ComboBox.
as it is stated here.
Update
Please use this one:
((ComboBoxItem)comboBox1.SelectedItem).Content.ToString();
Or in two steps:
ComboBoxItem item = (ComboBoxItem)comboBox1.SelectedItem;
timer1.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(item.Content.ToString()));
For more information about the ComboBoxItem class, please have a look here.
It appears that you are adding ComboBoxItems directly to your ComboBox.
A cleaner and safer approach than parsing strings would be to continue binding to SelectedItem, but to also bind the ItemsSource to a collection of integers.
Then use the ItemTemplate property of the ComboBox to define how to render the integers if you are not satisfied with the default ToString() rendering.
<ComboBox ItemsSource="{Binding Intervals}" SelectedItem="{SelectedInterval}">
<ComboBox.ItemTemplate>
<DataTemplate TargetType="{x:Type Int64}">
<TextBlock Text="{Binding}" Background="Red"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox>
With properties looking something like this:
public int SelectedInterval {get;set;}
public List<int> Intervals {
get {
var lst = new List<int>();
for(var i = 1000; i <= 10000; i += 500)
{
lst.Add(i);
}
return lst;
}
}
Now you have strongly type properties that you can manipulate without parsing.
I'm using Entity Framework as my database source and need to convert a Linq query "var" type to an ObservableCollection. I then need to bind the ObservableCollection to a ComboBox on WPF form; binding to ItemsSource, DisplayMemeberPath, SelectedValuePath and SelectedValue.
Here is Code:
using (PulseContext pc = new PulseContext())
{
var maritalcodes = from m in pc.CodeMaster
where m.Type == "16"
select new { m.Code, m.Description };
prop.ClientData.Options = new ObservableCollection<object>(maritalcodes);
}
Problem is the ComboBox is showing this as "{ Code = ????, Description = ???? }" instead of bind to code for value and description for display. What do I have to do to get the ComboBox to bind to the individual elements?
You need to set SelectedValuePath and DisplayMemberPath like this:
prop.ClientData.Options = new ObservableCollection<object>(maritalcodes);
prop.ClientData.Options.SelectedValuePath = "Code";
prop.ClientData.Options.DisplayMemberPath = "Description";
Or you can set them in xaml like this:
<ComboBox ItemsSource="{Binding Path=maritalcodes}"
SelectedValuePath="Code"
DisplayMemberPath="Description" />
<ComboBox ItemsSource="{Binding Path=maritalcodes}"
SelectedValuePath="Code"
DisplayMemberPath="Description"
SelectedValue="{Binding Path=Code}"/>
I hope this will help.
I'm trying to each element I add a unique name which I get from the loop. How can I accomplish it? The line 2 is where is the error.
foreach (var station in stations) {
TextBlock station.name = new TextBlock(); // error !!!
}
Try this instead...
TextBlock station = new TextBlock() { Name="Something" };
Maybe I'm misunderstanding what you want to do... Are you trying to create controls for each member of a collection? If that's what you're doing, try looking at a ListBox, or a more generic ItemsPresenter control, and the ItemTemplate property of that element.
For example, add a ListBox to your XAML, and assign stations as it's ItemsSource, then add a DataTemplate to represent the items.
<ListBox ItemsSource="{Binding stations}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Your trying to create an new instance of textblock on the name property of the textblock instance station.
That is definitely not going to work.
Try:
TextBlock station = new TextBlock();
station.name = "Whatever name you want";