Binding ComboBox with one Item define and one dynamic - wpf

I have ComboBox with one define ComboBoxItem and one with TextBox as template.
Something like this:
<ComboBox Grid.Column="1" Grid.Row="4" Margin="3" VerticalAlignment="Center" SelectedItem="{Binding Repeatable}">
<ComboBoxItem Content="unbounded"/>
<ListBoxItem>
<ListBoxItem.Template>
<ControlTemplate>
<TextBox Text="{Binding Repeatable, Mode=TwoWay}" Width="200"/>
</ControlTemplate>
</ListBoxItem.Template>
</ListBoxItem>
</ComboBox>
My problem is that i don't know ho to bind a Repeatable string property.
If Repeatable = unbounded, I've need select ConboBoxItem and, if other string need fill text property of TextBox.
On click of any item in ComboBox, the value must be shown as selected.
Don't know ho to do that. Thanks for answers and ideas.

Try this:
<ComboBox Text="{Binding Repeatable}" IsEditable="True">
<ComboBoxItem Content="unbounded"/>
</ComboBox>

Related

Binding a value to ConverterParameter

I have a combobox control in xaml. Whatever is selected in combobox, I want it to be appended to the text in below TextBox. How can this be achieved?
<TextBox Text="{Binding Entity.Value.Minvalue,
Converter={StaticResource ConverterName},
ConverterParameter=???,
Mode=TwoWay}"/>
Can we pass Entity/Property in ConverterParameter? Any pointers in the direction how it can be achieved are greatly appreciated.
Try this
<Grid x:Name="LayoutRoot" >
<ComboBox x:Name="Cmb" Background="Red" Height="35" Width="300" >
<ComboBoxItem Content="ComboBoxItem1"></ComboBoxItem>
<ComboBoxItem Content="ComboBoxItem1"></ComboBoxItem>
<ComboBoxItem Content="ComboBoxItem1"></ComboBoxItem>
<ComboBoxItem Content="ComboBoxItem1"></ComboBoxItem>
</ComboBox>
<TextBox Text="{Binding ElementName=Cmb,Path=SelectedValue.Content,Mode=TwoWay}" HorizontalAlignment="Right" VerticalAlignment="Bottom"></TextBox>
</Grid>
Mode you can decide as per your requirement..
If you want to append text every time the SelectedItem changes for the ComboBox, you can listen to the SelectionChanged event and update the text.
<StackPanel>
<ComboBox x:Name="Combo" ItemSource="{Binding MyItems}" SelectionChanged="OnComboBoxSelectionChanged">
<!-- define your template -->
</ComboBox>
<TextBlock x:Name="AppendingText" Text=""/>
</StackPanel>
In your code begind, in the OnComboBoxSelectionChanged, add to the text
// You probably want to cast SelectedItem to your model and get a property from it
AppendingText.Text += Combo.SelectedItem.ToString();

Multiselect ComboBox, Force it to always display the same text

I have a multiselect combobox that works fine. Except for the text. I want it to always have the same text ("Commodity Filter") regardless of what the user has selected.
If I set iseditable to true and the text to CommodityFilter it looks fine until the user makes a selection, then it is garbage (displays object type name). How can I hard code some text there? (Actually ideally i would databind it so it can change depending on whether anything is selected, but that would be a bonus at this point)
<ComboBox IsEditable="True" Text ="Commodity Filter" ItemsSource="{Binding Path=ActiveCommodities}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding Commodity}"
Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I ended up creating a custom object for populating the ComboBox (which had the IsSelected property and implemented INotifyPropertyChanged) because I was creating several comboboxes to control filtering. Once I did this is was trivial to override the tostring on the customobject and pass in the appropriate text. So the xaml did not change much.
I would have preferred to overlay with a text box but that seemed to be beyond my abilities to get a polished look in a reasonable time.
<ComboBox ItemsSource="{Binding Path=ActiveFuturesMonths}"
IsEditable="True"
IsReadOnly="True"
Text="Futures Month Filter" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding Text}"
Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Actually the crux is in setting -
IsEditable="True"
IsReadOnly="True"
Text="Futures Month Filter"
rather than creating custom object. thanks a lot it helped.

Lookup-id-control in WPF DataGrid

I am working with DataGrid. One column displays text, but the data behind this contains only an id. This id must somehow be converted to a string.
I need something like a combobox with the properties ItemsSource, DisplayMemberPath, SelectedValue and SelectedValuePath. But instead of a button displayed, there must be only a text. Is there some control for that?
That works (would like to exchange combobox with something that looks like textbox):
<DataGridTemplateColumn Header="Leistungsart" MinWidth="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=viewModel}, Path=Leistungsarten}"
DisplayMemberPath="Bezeichnung"
SelectedValue="{Binding Path=BDELeistungsartID, Mode=OneWay, Converter={StaticResource ResourceKey=NullableInt2IntConverter}}"
SelectedValuePath="BDELeistungsartID"
IsEnabled="false"
IsEditable="False"
Height="35">
</ComboBox>
</DataTemplate>
Thanks a lot for your aswer. Yes, that with Template property worked for me:
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=viewModel}, Path=Leistungsarten}"
DisplayMemberPath="Bezeichnung"
SelectedValue="{Binding Path=BDELeistungsartID, Mode=OneWay, Converter={StaticResource ResourceKey=NullableInt2IntConverter}}"
SelectedValuePath="BDELeistungsartID">
<ComboBox.Template>
<ControlTemplate>
<Label Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedItem.Bezeichnung}"
Margin="0,0,0,0" Padding="0,0,0,0"/>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
You could use a ComboBox, but overwrite the Template property to show just a Label. You'll have to recreate the Click events too.
Easiest way would be to use a tool like Snoop or Blend and see what the default ComboBox template looks like, and modify that to what you want.

ComboBoxItem to full width of ComboBox, when using SelectedIndex, or navigating keyboard?

Following XAML causes the "My stuff" being centered of ComboBox until I open the ComboBox, when it correctly stretches itself.
<ComboBox Height="30" Width="300" HorizontalContentAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem HorizontalContentAlignment="Stretch">
<Border Background="Red">
<TextBlock>My stuff...</TextBlock>
</Border>
</ComboBoxItem>
</ComboBox>
The question is, is it possible to get the ComboBoxItem being stretched even when it's selected using SelectedIndex? Same bug, or feature, happens if SelectedIndex is untouched (-1) and one selects the item using keyboard.
Workaround is probably to open the ComboBox programmatically in the beginning of app, which is rather ugly.
You just need to set the width of your border to the dynamic width of your outercontrol:
E.g.
Width="{Binding ElementName=combox1, Path=ActualWidth}">
Try this:
<ComboBox x:Name="combox1" Height="30" Width="300" HorizontalContentAlignment="Stretch"
SelectedIndex="0">
<ComboBoxItem HorizontalContentAlignment="Stretch">
<Border Background="Red" Width="{Binding ElementName=combox1, Path=ActualWidth}">
<TextBlock>My stuff...</TextBlock>
</Border>
</ComboBoxItem>
</ComboBox>
I see yes - i'm sure there is a way round this. It really depends what the end result is that you want. Do each of your data items have a difference background colour to identify them or is it simply a background colour to your whole combobox which you are trying to achieve.
If it is the latter, try this - and perhaps also remove the highlighted selection colour too, else perhaps the code behind route is correct, in terms of preselecting your first item, as that may be an option.
Example of All over Background colour:
<ComboBox Background="Red" x:Name="combox2" Height="30" HorizontalContentAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem Background="Red" HorizontalContentAlignment="Stretch">
<TextBlock Background="Red">My stuff...</TextBlock>
</ComboBoxItem>
</ComboBox>
Hope this helps! :)

WPF DataGrid - Problem Customizing a ComboBox

I have a Datagrid with a combo box bound column that works great as follows:
<tk:DataGridComboBoxColumn SelectedValueBinding="{Binding DefaultLocationID}"
SelectedValuePath="LocationID"
DisplayMemberPath="LocationName"
Header="Default Location"
ItemsSource="{Binding Source={StaticResource CustomerLocations}}">
</tk:DataGridComboBoxColumn>
Ultimately I want to customize the dropdown to show 'ID' and 'Name' together so a TemplateColumn seems to be the way to go. Starting small, I can't get the following simple example to work which should replace the standard DataGridComboBoxColumn. The dropdown appears fine, but when I 'select' a dropdown item, it does not accept and goes back to the previous value. I've tried variations on 'Mode=TwoWay' but no luck. Do I need a 'CellEditingTemplate' ?
DefaultLocationID is the foreign-key field being edited, while 'LocationID' is a column in 'CustomerLocations'.
<tk:DataGridTemplateColumn Header="Default Location">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox VerticalAlignment="Top"
SelectedItem="{Binding LocationID, Mode=TwoWay}"
SelectedValue="{Binding DefaultLocationID, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource CustomerLocations}}"
SelectedValuePath="LocationID"
DisplayMemberPath="LocationName" >
</ComboBox>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
Thanks!!
Can you post the relevant parts of your CustomerLocations resource? Can you also post the type of class that your grid is binding to?
Try removing the SelectedValuePath, DisplayMemberPath and SelectedValue from the ComboBox.
If you want to display multiple pieces of data in your combobox see the below XAML
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LocationId}"} />
<TextBlock Text=" - "/>
<TextBlock Text="{Binding LocationName}"} />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Resources