I have been defined some ComboBox element:
<ComboBox Height="27" Margin="124,0,30,116" Name="cbProductDefaultVatRate" VerticalAlignment="Bottom" ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Height="26" Content="{Binding Path=Value}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and set the data source for component items of VatRate type:
private void ShowAllVatRates()
{
cbProductDefaultVatRate.Items.Clear();
cbProductDefaultVatRate.ItemsSource = new VatRateRepository().GetAll();
}
VatRate object has a property:
private Product SelectedProduct
{
get; set;
}
where is a product contains VatRate as well:
SelectedProduct.DefaultVatRate
How to set SelectedItem property of ComboBox to SelectedProduct.DefaultVatRate?
// does not work!!!
cbProductDefaultVatRate.SelectedItem = SelectedProduct.DefaultVatRate;
Thank you for answers!
You need to make sure that the actual object instance behind SelectedProduct.DefaultVatRate is the same instance as the one that is part of the list returned by new VatRateRepository().GetAll() or object.Equals() must return true for the two instances.
Are you looking to get a TwoWay binding like this?
<ComboBox Height="27" Margin="124,0,30,116" Name="cbProductDefaultVatRate" VerticalAlignment="Bottom"
ItemsSource="{Binding}"
SelectedItem="{Binding SelectedProduct.DefaultVatRate, Mode=TwoWay}>
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Height="26" Content="{Binding Path=Value}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Related
I have a little problem and I don't know how to fix it.
In my View I have a combobox, that should be able to display the property "Name" of the ObservableCollection "Phases". I already tried to show a single "Name-property" without a Datatemplate and it worked. I think I messed something up with the binding in the DataTemplate. What is wrong? Can you help me?
Here is my View:
<ComboBox ItemsSource="{Binding Phases}"
SelectedItem=""
Width="100" HorizontalAlignment="Left"
Margin="50,20">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here my ObservableCollection:
public ObservableCollection<PhaseViewModel> Phases
{
get;
}
And here the property inside Phases:
public string Name
{
get { return myName; }
set { myName = value;}
}
Hy Everyone!
My situation so far:
I have an observable collection ("PipesSystemList") which is made up of "DataBaseSystem" objects:
public static ObservableCollection<DatabaseSystem> PipesSystemList { get; set; } = new ObservableCollection<DatabaseSystem>();
public class DatabaseSystem
{
public string System { get; set; }
public bool IsChecked { get;set;}
}
I successfully bind this collection to my combobox as it follows and everything works perfectly:
<ComboBox ItemsSource="{Binding ElementName=MainView,Path=DataContext.PipesSystemList}"
Padding="2" Margin="5 0 5 0" Width="70" Height="20"
Foreground ="#F1F1F1" Style="{StaticResource ComboBoxStyleDark}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="local:DatabaseSystem">
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding System}" Foreground="#F1F1F1"
IsChecked="{Binding IsChecked}"
Command="{Binding ElementName=MainView, Path=DataContext.PipeTabChosenSystemChangedCommand }"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
What I wanted to achieve: Because I have multiple equipment type, I wanted to have all the necesarry collection in one place. so I created a dictionary:
var SystemTabLists = new Dictionary<string, ObservableCollection<DatabaseSystem>>()
{
{"Document List",new ObservableCollection<DatabaseSystem>()},
{"Pipes",new ObservableCollection<DatabaseSystem>()},
{"Valves",new ObservableCollection<DatabaseSystem>()},
{"Sensors",new ObservableCollection<DatabaseSystem>()},
{"Vessels",new ObservableCollection<DatabaseSystem>()},
{"Heat Exchangers",new ObservableCollection<DatabaseSystem>()},
{"Filters",new ObservableCollection<DatabaseSystem>()},
{"Other Equipment",new ObservableCollection<DatabaseSystem>()}
};
After this, I tried to bind the combobox to the collection belonging to the Pipes (I successfully managed to fill up the collection with the right elements, so the data I need is there):
<ComboBox ItemsSource="{Binding ElementName=MainView,Path=DataContext.SystemTabLists[Pipes]}"
Padding="2" Margin="5 0 5 0" Width="70" Height="20"
Foreground ="#F1F1F1" Style="{StaticResource ComboBoxStyleDark}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="local:DatabaseSystem">
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding System}" Foreground="#F1F1F1"
IsChecked="{Binding IsChecked}"
Command="{Binding ElementName=MainView, Path=DataContext.PipeTabChosenSystemChangedCommand }"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Unfortunately this is not working.
When I debug the code and I turn on tracing for bindings I receive this:
"BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=DataContext.SystemTabLists[Pipes]"
Could someone help me what am I doing wrong? Or point out if this is not possible to do.
Thanks in advance.
So I'm trying to do a program that uses ListView (instead of CheckedListBox), and I want to get the indexes of the checked elements.
I use this as a template for the ListView:
<ListView x:Name="Content" HorizontalAlignment="Left" Height="303" Margin="10,47,0,0" VerticalAlignment="Top" Width="394" ItemClick="Content_ItemClick" SelectionChanged="Content_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Selected}"></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And this is the data what it got:
public class User
{
public string Name { get; set; }
public bool Selected { get; set; }
}
How could I get back the checked elements index?
I believe your ListView's item source is bound to a backing list
<ListView ItemSource"{x:Bind UserList, Mode=OneWay}" >
<ListItem.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Selected, Mode=TwoWay} Click="Click_Handler"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListItem.ItemTemplate>
</ListView>
The UserList is List<User> which contains all your User.
Since you bind the UserList to the ListView. You could retrieve the selected item by iterating through the list and read the Selected property of each individual User.
I have the following collection which i would like to bind to a combobox:
public ObservableCollection<Parameter> Values
{ get; set; }
public class Parameter
{
public String Text { get; set; }
public String Value { get; set; }
}
I need to bind the display text of the combobox to the Text property of the Parameter class, I've tried the following ways below but all to no avail:
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Parameter.Text"
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Parameter\Text"
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text"
When I try the 3 methods above the [assembly name].Parameter is displayed in the combobox for each parameter.
The 2 methods above don't display anything in the combobox
<ComboBox ItemsSource="{Binding Values, Path=Text}"
<ComboBox ItemsSource="{Binding Values, Path=Values.Text}"
And this one takes the text of the first parameter,splits it into characters and displays each character as a seperate item in the combobox:
<ComboBox ItemsSource="{Binding Values, Path=Values/Text}"
UPDATE:
This is the complete XAML code as requested
<ListBox BorderBrush="{x:Null}" Grid.Column="0" Height="100" Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding ItemParams}" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="2" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock TextTrimming="CharacterEllipsis" Grid.Column="0" Margin="2" Text="{Binding Name}" Background="{Binding ElementName=cmbColors, Path=SelectedItem}"/>
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text" SelectedIndex="0" HorizontalAlignment="Left" Grid.Column="1" Margin="2" Width="150" Name="cmbColors" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Params class:
public class Params
{
public Params(String name, ObservableCollection<Parameter> values)
{
Name = name;
Values = values;
}
public String Name
{ get; set; }
public ObservableCollection<Parameter> Values
{ get; set; }
}
The correct formulation is one of those you already mention:
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text"
Please check again, and if it does not work post the XAML that you have verbatim.
You can do directly :
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text" />
For WPF every element of the ComboBox (ComboBoxItem) is a of type T, thus it will look for public properties on the T, this why just putting Text will work.
Had to simply remove the combobox item template or set Text="{Binding Text}"
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
So I have a ComboBox with data in it and it works how they want:
<ComboBox Grid.Column="1" x:Name="MyComboBox" Margin="2, 0, 2, 0"
ItemsSource="{Binding Path=MySamples}" DisplayMemberPath="SampleName" SelectedValue="{Binding Path=MySample}"
SelectionChanged="OnComboBoxChanged" FontSize="11" FontFamily="Arial"/>
However, now they want the ItemsSource to be indexed. So it should be something like:
some #: SampleName
Is there an easy way to make this change just for the ComboBox drop down without changing the architecture? I cannot change the List itself since in other areas of the map, it's just the SampleName without the index. Thanks.
If your ItemsSource is a complex type:
public class MyClass
{
public int Index { get; set; }
public string Name { get; set; }
}
Then use the DisplayMemberPath property of the ComboBox to control what gets displayed. In this case you'd add:
DisplayMemberPath="SampleName"
to the ComboBox definition.
If instead you want to display both the index and name then you'll need to define an ItemTemplate for the ComboBox:
<ComboBox ....>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Index}" />
<TextBlock Text=" : " />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>