Create dynamic combobox JavaFx with FXML - combobox

I'm trying to create a Combobox with JavaFX and FXML with dynamic items.
Right now I have the next code, and it's rendering the combo with a list of object identifiers values. For example: my.models.Province#1ac312.
<ComboBox fx:id="provinceCombo">
<items>
<FXCollections fx:factory="observableArrayList" />
</items>
</ComboBox>
I'm loading the combo in my controller like this:
#FXML
private ComboBox<Province> provinceCombo;
#Override
public void initialize(URL url, ResourceBundle rb) {
this.provinceCombo.getItems().addAll(ProvinceService.getProvinces());
}
But of course, my combo doens't know which property of my object Province should display in the combo and which property should choose as value.
How can I define this in my FXML? I don't find anything on the internet with FXML.
Thanks!

In your Province class, override the toString() method:
#Override
public String toString(){
return name; //or whatever you want :)
}

Related

WPF/XAML: Shortcut to a user control

I have created a UserControl named ContactPerson. It contains a persons name, phone number etc.
This usercontrol does not have a headline (ie. a label such as "_Contact Person") because I use this usercontrol in different situations.
However in one situation, I do have such a label, which means my code look somewhat like this:
<Label Content="_Contact Person"
Target="{Binding ElementName=_contactView}" />
<View1:contactView x:Name="_contactView"
DataContext="{Binding SupplierContact}"/>
I want - to set keyboard focus to the name-textbox inside the ContactPersonUserControl but it seems to be a difficult task (it is private after alle).
I do not want to move the label inside the usercontrol, which I guess would in fact be the most simple solution. It seems to me that XAML should provide a solution to this scenario.
How to do this in a simple and elegant way?
Thx
(I have a few of these controls, so I'll need to use the same solution several times).
I am not sure whether i understand your scenario correctly, But if you want to set focus to ui control from view model, then you may need to create an attached property.
Attached Property:
public static class ControlFocusExtension
{
public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached("IsFocused", typeof(bool), typeof(ControlFocusExtension), new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
private static void OnIsFocusedPropertyChanged(DependencyObject pDependencyObject, DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)pDependencyObject;
if ((bool)e.NewValue)
{
uie.Focus();
}
}
}
User control XAML:
Now in your View (in XAML) you can bind this property to your ViewModel:
<TextBox local:FocusExtension.IsFocused="{Binding IsNameFocused}" />
You can change the value of IsNameFocused from your view model if needed.
Alternatively if you don't want to bind this then you can use it as -
<TextBox local:FocusExtension.IsFocused="True" />
Set focusvisualstyle property null for those you dont want to set focus like this
example <Label FocusVisualStyle="{x:Null}">
and for textbox focus <TextBox Focusable="True"/>

zk: combobox model binding issue

I have a model which i have attached to combobox, in browser I can see the labels as name of kpis. problem is when I get the model after save, everything is there but this combobox value for kpiFrequency.kpi or kpiDTO value in kpiFrequency. I want any of the object filled with selected kpifrequency along with associated kpi object.
here is my code:
.zul file
<combobox id="kpiCombobox" model="#load(kf.kpiList)" readonly="true" maxlength="40" width="80%"
onChange="#command('onChnageKpiHeaderLabel')" selectedItem="#bind(kf.kpiFrequency.kpi.kpiName)">
<template name="model" var="kpiDTO">
<comboitem label="#load(kpiDTO.kpi.kpiName)" />
</template>
</combobox>
my DTO snap,
public class KpiFrequencyDTO {
private KPIFrequency kpiFrequency;
private List<KPIFrequency> kpiFrequencyList;
private List<String> frequencyNameList;
private List<String> typeList;
private List<String> aggFormulaList;
private List<KpiDTO> kpiList;
private KpiDTO kpiDTO;
ANSWER
I am adding the answer which worked for me. in combobox, rather then using selectedItem, use value, it solves the problem i was faceing.
See Zk Combobox With Selectoption
You will get the logic behind the ZK Combobox and how you have to made changes in your code .

Data binding access in xaml vs in code behind - Linq to XML?

I have a listbox that binds to and displays the Name elements from an XML file. When a listbox item is selected, I want to display the Price value associated with this item in a textblock. How do I retrieve the Price programmatically (meaning not in the xaml file but in code behind)? Thanks.
XML file has these nodes:
<Product>
<Name>Book</Name>
<Price>7</Price>
</Product>
I use Linq and do the select with an anonymous type. If the easiest way to access the field programmatically is through a named type, please show me how.
Here's how I bind in xaml (using a data template for each listbox item that contains):
<TextBlock Text = "{Binding Name}" />
Here's the code-behind function where I want to retrieve the Price:
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// how do I get the value of Price of the selected item here?
}
Please note that I want to access Price in this function, NOT in xaml!
First of all you probably don't even need LINQ as you can do a lot of things with XmlDocuments including doing selection via XPath (also in Bindings).
Secondly converting anonymous types to named types is trivial, if you have
select new { Name = ..., Price = ... }
You just need a class with the respective properties
select new Product { Name = ..., Price = ... }
public class Product
{
public string Name { get; set; }
public string Price { get; set; } // Datatype is up to you...
}
Thirdly you can make do without named types using dynamic.
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = (ListBox)sender;
// Named type:
Product item = (Product)listBox.SelectedItem;
// Anonymous type:
dynamic item = listBox.SelectedItem;
// <Do something with item.Price, may need to cast it when using dynamic>
// e.g. MessageBox.Show((string)item.Price);
}
You should be able to retrieve the selected item from the SelectionChangedEventArgs parameter. i.e.
var item = e.AddedItems.First();
Refer to this post - bind textblock to current listbox item in pure xaml, you can get the name both in xaml and code-behind using XmlDataProvider.

Silverlight Combobox - items with a command

What would be the best way to get the elements of a combobox to each support a Command and CommandParameter?
I'd like to implement the Theme Chooser shown toward the bottom of this blog post, except with a combo box instead of a context menu. I'd need each element of the combobox to support a Command and CommandParameter, and I'd like it to just be plain text, as the combo below is.
<ComboBox>
<ComboBox.Items>
<TextBlock>A</TextBlock>
<TextBlock>B</TextBlock>
<TextBlock>C</TextBlock>
</ComboBox.Items>
</ComboBox>
I tried hyperlinks, but the main problem there is that when you click directly onto the link text, the combo box does not close.
Is there an easy way to do this?
EDIT
Ok, well the specific goal that I said I wanted to achieve—having a combo change the SL Toolkit theme—is trivially accomplished. I can simply bind the selected item of the combo to a ViewModel property that then exposes the appropriate themeuri which my SL Toolkit theme can bind to, or, since this is purely a UI activity with no business logic, I can just catch the combobox item changed event, and update my themeUri from there.
I am curious though, is there a good way to bind each combo box item to a command with a command parameter? Using a Hyperlink as each comboboxItem seemed promising, but that prevents the CB from closing after you click on an item when you click the actual hyperlink.
You could Bind the selected item to your ViewModel and then the setter would trigger when the Theme was changed.
Xaml:
<ComboBox SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" ItemsSource="{Binding Themes}" />
CodeBehind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
DataContext = new MainPageViewModel();
}
}
ViewModel:
public class MainPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<string> Themes { get; set; }
private string _selectedTheme;
public string SelectedTheme
{
get { return _selectedTheme; }
set
{
_selectedTheme = value;
// Change the Theme
RaisePropertyChanged("SelectedTheme");
}
}
public MainPageViewModel()
{
Themes = new ObservableCollection<string>();
Themes.Add("Red");
Themes.Add("Green");
Themes.Add("Blue");
}
}

Silverlight - how do I get the text of the selected item in a combobox

Easy one for you all...
I'm new to Silverlight and really missing stuff like DataTables and things. What I'm also currently struggling with is how to get the text of my combobox's currently selected item.
In winforms I would have done:
ComboBox myCombo = new ComboBox.......
string selected = myCombo.Text;
I'm struggling how to get this info out.
The selected item of your combo box is whatever type of item is currently holding. So if you set the binding to a collection of strings, then the selected item will be a string:
string mySelectedValue = ((string)MyComboBox.SelectedItem);
If it is a more complex object you will need to cast and use the expected object. If you have XAML using the list box item class, like:
<ComboBox x:Name="MyComboBox">
<ComboBox.Items>
<ComboBoxItem>
<TextBlock Text="Hello World"/>
</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
Then you would access the selected item like this:
string mySelectedValue =
((TextBlock)((ComboBoxItem)MyComboBox.SelectedItem).Content).Text;
Right, the answer is to use myCombo.SelectionBoxItem.ToString()
For a complex object, use reflection with the DisplayMemberPath property:
var itemType = cbx.SelectedItem.GetType();
var pi = itemType.GetProperty(cbx.DisplayMemberPath);
var stringValue = pi.GetValue(cbx.SelectedItem, null).ToString();
((ComboBoxItem)comboBox1.SelectedItem).Content.ToString()
I got it worked by this statement.
string txt=(comboboxID.SelectedItem as BindingClass).Text.ToString();
string value=(comboboxID.SelectedItem as BindingClass).Value.ToString();
public class BindingClass
{
public string Text
{
set;
get;
}
public string Value
{
set;
get;
}
}
If you have a simple combobox for an array of strings, you can get the selected string using
(string)e.AddedItems[0];
Suppose I have a product list combo and I want to know the selected product name. So in the SelectionChanged Event I write the following code:
private void productCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string product_type=(string)e.AddedItems[0];
}
myCombo.SelectedItem.Content
will return the content of the ComboBoxItem. This could be a TextBlock, etc. depending on what you have in there, and what you are using for an item template.

Resources