There is a way how to set default value to ComboBox on JavaFx fxml.
I found sulution here:
https://stackoverflow.com/a/14436371/1344424
<ComboBox editable="true">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="NVT" />
<String fx:value="Bezig" />
<String fx:value="Positief" />
<String fx:value="Negatief" />
</FXCollections>
</items>
<value>
<String fx:value="NVT" />
</value>
</ComboBox>
But it not working when Editable property set to true.
How can I set default value to editable ComboBox?
It seems to work if you set the value via an attribute, instead of an element:
<ComboBox editable="true" value="NVT">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="NVT" />
<String fx:value="Bezig" />
<String fx:value="Positief" />
<String fx:value="Negatief" />
</FXCollections>
</items>
</ComboBox>
It also works this way if you pass in a reference to the same string:
<ComboBox editable="true" value="$defaultSelection">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:id="defaultSelection" fx:value="NVT" />
<String fx:value="Bezig" />
<String fx:value="Positief" />
<String fx:value="Negatief" />
</FXCollections>
</items>
</ComboBox>
I fear this is not possible with FXML, but in Java code:
ComboBox<String> combobox = new ComboBox<>(FXCollections.observableArrayList("1","2","3","4","5"));
combobox.setEditable(true);
combobox.getSelectionModel().selectFirst();
Or if you want to select a specific value:
combobox.getSelectionModel().select("3");
Related
I have the following XML structure(simplified for example's sake) where you can have questions within questions. There are other types of questions so I have decided to make a template to display them, I can create the top level of questions show, but when i get to the inner levels I can't get my template to show. I do have other question types other than "string", but I removed them to try to focus on the issue at hand.
The XML structure
<QUESTIONS>
<QUESTION id="3" text="What is this?" type="string">
<QUESTIONS>
<QUESTION id="7" text="What is the First Inner Question?" type="string">
<QUESTIONS>
<QUESTION id="8" text="What is another inner Question?" type="string">
</QUESTION>
</QUESTIONS>
</QUESTION>
</QUESTIONS>
</QUESTION>
<QUESTION id="4" text="Where another question?" type="string">
<QUESTIONS>
<QUESTION id="5" text="What is another inner question?" type="string">
<QUESTIONS>
<QUESTION id="6" text="What is another inner question?" type="string">
</QUESTION>
</QUESTIONS>
</QUESTION>
</QUESTIONS>
</QUESTION>
</QUESTIONS>
My XAML
<Window.Resources>
<DataTemplate x:Key="StringQuestionTemplate" DataType="models:FormQuestion" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=QuestionText}" />
<TextBox Text="{Binding Path=Answer, Mode=TwoWay}" />
<StackPanel>
<ItemsControl ItemsSource="{Binding Path=InnerQuestions}" ItemTemplateSelector="{StaticResource TemplateSelector}" />
</StackPanel>
</StackPanel>
</DataTemplate>
<templateIssue:QuestionTemplateSelector x:Key="TemplateSelector"
StringTemplate="{ StaticResource StringQuestionTemplate }" />
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding Path=Questions}" ItemTemplateSelector="{StaticResource TemplateSelector}" >
</ItemsControl>
</StackPanel>
</Grid>
Here is the QuestionTemplateSelector
public class QuestionTemplateSelector : DataTemplateSelector
{
public DataTemplate StringTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
switch (((FormQuestion)item).Type)
{
case QuestionType.StringQuestion:
return StringTemplate;
default:
return StringTemplate;
}
}
}
I would like to use the same template selector for the "InnerQuestions" as i do for the original templates, but since it is referenced after the section I am not able to(an error is thrown). So I would like to use the same DataTemplate In the "InnerQuestions" as i do for the top level questions. Is there a way to accomplish this, either templating it using a different structure?
It sounds like you need to use the HierarchicalDataTemplate Class. Something like this perhaps (assuming your Bindings are correct):
<HierarchicalDataTemplate DataType="models:FormQuestion"
ItemsSource="{Binding Path=InnerQuestions}"
ItemTemplateSelector="{StaticResource TemplateSelector}">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=QuestionText}" />
<TextBox Text="{Binding Path=Answer, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
UPDATE >>>
You also need to use a control that can display hierarchical data, like a TreeView:
<TreeView ItemsSource="{Binding TopLevelQuestions}" />
I am a newbie with WPF.
I have a combobox bound to an XML datasource defined in the Window.Resources.
The combobox values are shown in the designer, but it comes out empty at runtime.
Am I missing something here.
<Window x:Class="WpfExample4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="xmlData">
<x:XData>
<customers>
<customer name="Customer 1">
<order desc="Big Order">
<orderDetail product="Glue" quantity="21" />
<orderDetail product="Fudge" quantity="32" />
</order>
</customer>
<customer name="Customer 2">
<order desc="First Order">
<orderDetail product="Mousetrap" quantity="4" />
</order>
</customer>
</customers>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid DataContext= "{Binding Source={StaticResource xmlData}, XPath=customers/customer}" Margin="4" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- CUSTOMERS -->
<DockPanel Grid.Row="0">
<TextBlock DockPanel.Dock="Top" FontWeight="Bold" Text="Customers" />
<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=#name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DockPanel>
</Grid>
</Window>
You should add namespace to root element (msdn).
Note:
The root node of the XML data has an xmlns attribute that sets the XML
namespace to an empty string. This is a requirement for applying XPath
queries to a data island that is inline within the XAML page. In this
inline case, the XAML, and thus the data island, inherits the
System.Windows namespace. Because of this, you need to set the
namespace blank to keep XPath queries from being qualified by the
System.Windows namespace, which would misdirect the queries.
...
<Window.Resources>
<XmlDataProvider x:Key="xmlData">
<x:XData>
<customers xmlns="">
<customer name="Customer 1">
<order desc="Big Order">
<orderDetail product="Glue" quantity="21" />
<orderDetail product="Fudge" quantity="32" />
</order>
</customer>
...
have my store and my grid panel, i need to add a filter box to it , so added a topbar tag, that has the field box to filter.. and want to add listeners on it so i would be filterer while writing, the 2 functions as shown are filtergrid() and clearFilter()...what those 2 functions should be, knowing that the store is binded with code behind...
<ext:Store ID="Store1" runat="server">
<Reader>
<ext:JsonReader>
<Fields>
<ext:RecordField Name="FULLNAME" />
<ext:RecordField Name="JOBTITLE" />
<ext:RecordField Name="PHONENUMBER1" />
</Fields>
</ext:JsonReader>
</Reader>
</ext:Store>
<ext:Panel ID="Panel1" runat="server" Width="520" Height="300" Collapsible="true" Title="Add Attendies" Collapsed="true">
<TopBar>
<ext:Toolbar ID="Toolbar2" runat="server">
<Items>
<ext:ToolbarTextItem ID="ToolbarTextItem1" runat="server" Text="Filter:" />
<ext:ToolbarSpacer />
<ext:TriggerField ID="TriggerField1" runat="server" EnableKeyEvents="true">
<Triggers>
<ext:FieldTrigger Icon="Clear" />
</Triggers>
<Listeners>
**<KeyUp Fn="filtergrid()" Buffer="250" />
<TriggerClick Handler="clearFilter();" />**
</Listeners>
</ext:TriggerField>
</Items>
</ext:Toolbar>
</TopBar>
<Items>
<ext:BorderLayout ID="BorderLayout1" runat="server">
<West MarginsSummary="5 5 5 5">
<ext:GridPanel
ID="GridPanel1"
runat="server"
StoreID="Store1"
DDGroup="GridDDGroup"
EnableDragDrop="true"
StripeRows="true"
AutoExpandColumn="FULLNAME"
Width="250"
Title="All Contacts">
<ColumnModel>
<Columns>
<ext:Column Header="Contact Name" ColumnID="FULLNAME" Width="140" DataIndex="FULLNAME" />
<ext:Column Header="Title" ColumnID="JOBTITLE" Width="75" DataIndex="JOBTITLE" />
<ext:Column Header="Phone" ColumnID="PHONENUMBER1" Width="75" DataIndex="PHONENUMBER1" />
</Columns>
</ColumnModel>
<SelectionModel>
<ext:RowSelectionModel ID="RowSelectionModel1" runat="server" />
</SelectionModel>
<GetDragDropText Fn="getDragDropText" />
<Listeners>
<Render Fn="setDD" />
</Listeners>
</ext:GridPanel>
</West>
<Center MarginsSummary="5 5 5 0">
<ext:GridPanel
ID="GridPanel2"
runat="server"
StoreID="Store2"
DDGroup="GridDDGroup"
EnableDragDrop="true"
StripeRows="true"
AutoExpandColumn="FULLNAME"
Width="250"
Title="Meeting Attendies">
<ColumnModel>
<Columns>
<ext:Column ColumnID="Contact Name" Header="Contact Name" Width="140" DataIndex="FULLNAME" />
<ext:Column Header="Title" Width="75" DataIndex="JOBTITLE" />
<ext:Column Header="Phone Number" Width="75" DataIndex="PHONENUMBER1" />
</Columns>
</ColumnModel>
<SelectionModel>
<ext:RowSelectionModel ID="RowSelectionModel2" runat="server" />
</SelectionModel>
<GetDragDropText Fn="getDragDropText" />
<Listeners>
<Render Fn="setDD" />
</Listeners>
</ext:GridPanel>
</Center>
</ext:BorderLayout>
</Items>
<BottomBar>
<ext:Toolbar ID="Toolbar1" runat="server">
<Items>
<ext:ToolbarFill ID="ToolbarFill1" runat="server" />
<ext:Button ID="Button1" runat="server" Text="Reset both grids">
<Listeners>
<Click Handler="Store1.loadData(Store1.proxy.data);Store2.removeAll();" />
</Listeners>
</ext:Button>
</Items>
</ext:Toolbar>
</BottomBar>
</ext:Panel>
so there is 2 functions that i have to write , one that filter an return the result to be written in the panel, and the other one is to clear and restore the datasource again.
You can apply a filter using the filterBy method and passing a delegate function which returns true or false based on some logic of your choosing and then remove the filter using clearFilter
In mode cody terms, something like this:
clearFilter() {
theGridPanel.store.clearFilter(false);
}
filterGrid() {
theGridPanel.store.filterBy(theFilterFunction)
}
theFilterFunction(record, id) {
if(record.data.aField === 'yellow') {
return true;
}
return false;
}
I have a WPF Combobox defined as such:
<ComboBox Grid.Column="1" x:Name="cUrls" SelectedIndex="1" ItemsSource=" {Binding XPath=//data/endpoints/endpoint}" Margin="5" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=#name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The window is bound to an XmlDocument like this:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<endpoints>
<endpoint name="test">test url</endpoint>
<endpoint default="true" name="production">production url</endpoint>
</endpoints>
<requests>
<request >
...
</request>
<request >
...
</request>
</requests>
</data>
The binding works fine and the combo box shows the items "test" and "production" and I am able to pull the right URL out of the SelectedValue property.
I would like to be able to set the SelectedIndex property on the ComboBox to the index of the <endpoint> node that has default=true attribute.
Can I do SelectedIndex="{Binding XPath=}" on the ComboBox? If yes, what would that expression look like? If not, what should I do?
Thanks!
Try
<ComboBox x:Name="cUrls"
SelectedItem="{Binding XPath=/data/endpoints/endpoint[#default\=\'true\']}"
I am a little unclear on how I would set the SelectedValuePath of a TabControl to the text of the selected TabItems header. I feel like this should be fairly simple, and probably involves content.something but I have always been a bit confused about the Content property.
ItemTemplate — template for tab headers. Just add textblock with the same binding as in property SelectedValuePath.
<UserControl.Resources>
<XmlDataProvider x:Key="Employees" XPath="/Employees/*">
<x:XData>
<Employees xmlns="">
<Employee Name="Terry Adams" Type="FTE" EmployeeNumber="1" />
<Employee Name="Claire O'Donnell" Type="FTE" EmployeeNumber="12345" />
<Employee Name="Palle Peterson" Type="FTE" EmployeeNumber="5678" />
<Employee Name="Amy E. Alberts" Type="CSG" EmployeeNumber="99222" />
<Employee Name="Stefan Hesse" Type="Vendor" EmployeeNumber="-" />
</Employees>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="HeaderDataTemplate">
<TextBlock Text="{Binding XPath=#EmployeeNumber}" />
</DataTemplate>
<DataTemplate x:Key="ContentDataTemplate">
<TextBlock Text="{Binding XPath=#Name}" />
</DataTemplate>
</UserControl.Resources>
<TabControl ItemsSource="{Binding Source={StaticResource Employees}}"
ItemTemplate="{StaticResource HeaderDataTemplate}"
ContentTemplate="{StaticResource ContentDataTemplate}"
SelectedValue="12345"
SelectedValuePath="#EmployeeNumber"/>