I created a new project using template10 and I'm working to a simple form: I load a list of items from a remote server in a comboxbox and select one of the items after the list is loaded.
I tried setting SelectedValue, SelectedItem ot SelectedIndex, but when the Form is shown the listbox appears unselected.
Am I missing something?
this is the xaml
<ComboBox x:Name="voceSpesaCb" Margin="16,16,0,0"
RelativePanel.AlignLeftWith="parameterResizer"
RelativePanel.Below="voceSpesaTextBlock"
DisplayMemberPath="Descrizione"
SelectedValue="{x:Bind ViewModel.VoceCorrente, Converter={StaticResource XConverter}, Mode=TwoWay}"
ItemsSource="{x:Bind ViewModel.Voci, Converter={StaticResource XConverter}}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
this is the code
Voci = await vociSpesaTable.OrderBy(vs => vs.Descrizione).ToListAsync();
VoceCorrente = Voci.FirstOrDefault(vs => vs.VoceSpesaNo == Item.VoceSpesaNo);
in the setter of the properties there is the call to the RaisePropertyChanged
Looks loike I found the problem. For reasons I don't understand at full I have to set Mode=TwoWay for the ItemsSource property too.
<ComboBox x:Name="voceSpesaCb" Margin="16,16,0,0"
RelativePanel.AlignLeftWith="parameterResizer"
RelativePanel.Below="voceSpesaTextBlock"
DisplayMemberPath="Descrizione"
SelectedValue="{x:Bind ViewModel.VoceCorrente, Converter={StaticResource XConverter}, Mode=TwoWay}"
ItemsSource="{x:Bind ViewModel.Voci, Converter={StaticResource XConverter}, Mode=TwoWay}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
/>
This way it works
Related
Let me explain so I have a wpf application I used a listBox with a template. This template contains a TextBlock and a ComboBox. When running the application, everything goes well, my list is initialized correctly. But then I would like to retrieve the values of my TextBlock and my comboBox and I don't understand how I can achieve this.
I am attaching the part of my XAML code that deals with this listBox :
<ListBox x:Name="ListBoxEnv" Grid.Row="1"
d:ItemsSource="{d:SampleData ItemCount=5}" Width="460"
SelectionChanged="ListBoxEnv_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="TxtBlockEnv"
VerticalAlignment="Center" HorizontalAlignment="Left"
Text="{Binding EnvName}"/>
<ComboBox x:Name="ComboBoxEnv"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding EnvListValue}"
Margin="100,2,0,2" Width="200"
SelectionChanged="ComboBoxEnv_SelectionChanged"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The EnvName property is readonly because it is bound to TextBlock, so you can ignore it. Change binding to: Text="{Binding EnvName, Mode=OneTime}" to save the app resources.
However to extract selected environment in every combo in the list you need to add to ComboBox template: SelectedItem={Binding SelectedEnv} and add new property to SampleData
for example
public MyEnvironmentClass SelectedEnv {get; set;}
I have a ComboBox with a list of names of objects. When I perform my rename command, the selected item's name changes in the list of items but will not show the updated name at the top unless I click to a different object then back. Here is a picture of the problem:
Here is my ComboBox xaml:
<ComboBox Name="CSCB" IsEditable="True" IsReadOnly="True" Margin="8"
ItemsSource="{Binding Systems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedCoord, Mode=TwoWay}"
Text="Select a Coordinate System"
/>
Let me know if I should include more code. Thank you :)
Unfortunately, you have to either re-implement a custom combobox that do that, or you could simply refresh the combobox items manually on the TextChanged event like so:
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
CSCB.Items.Refresh();
}
Assuming your xaml looks something like that:
<StackPanel>
<ComboBox Name="CSCB" IsEditable="True" IsReadOnly="True" Margin="8"
ItemsSource="{Binding Systems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedCoord, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Text="Select a Coordinate System"
/>
<TextBox Text="{Binding SelectedCoord.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBoxBase_OnTextChanged"></TextBox>
</StackPanel>
I'm trying to organize the items in a combobox into groups. To do this I've created an object that has project and group name strings. I then set the GroupStyle and ItemTemplate to display these values. However, Currently, only the project string is displayed in the combobox (and the box has a red border, indicating some kind of error).
Here's the xaml for my combobox:
<ComboBox x:Name="comboBoxProjects" Margin="165,90,28,0" Grid.Column="0" VerticalAlignment="Top" Height="25"
IsSynchronizedWithCurrentItem="True" SelectedIndex="0" Style="{StaticResource ComboBoxDefault}"
ItemsSource="{Binding Path=ProjectClientSelections.ProjectGroupItems,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=ProjectClientSelections.SelectedProject, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding GroupName}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Project}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Does anyone see where I'm going wrong?
In GroupStyle, the DataContext is not your item (the type contained in your ItemsSource), but a CollectionViewGroup object, which is formed based on the collection of items that you have grouped. Because of this you have to declare a binding path to one of the properties in CollectionViewGroup, for example, based on your code you probably want to use Name property. See MSDN CollectionViewGroup Class
Change your GroupStyle.HeaderTemplate to this:
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
You don't show how you have formed your GroupDescriptions. If you have not grouped the items already, you can do it in following way (assuming the XAML you have provided is contained inside Window and Window's and GroupBox's DataContext is the same):
<Window.Resources>
<CollectionViewSource
Source="{Binding ProjectClientSelections.ProjectGroupItems}"
x:Key="GroupedProjectItems">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription
PropertyName="GroupName" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
After this change GroupBox ItemSource binding to the following (directly to CollectionViewSource resource):
ItemsSource="{Binding Source={StaticResource GroupedProjectItems}}"
To display items on Telerik:RadGridView usually I use DataContext="{Binding [someViewModel]}" and ItemSource="{Binding objectList, Mode=TwoWay}".
and for my column I'll access the objectfield. The overall picture will be something like below:
<telerik:RadGridView DataContext="{Binding [someViewModel]}"
ItemSource="{Binding objectList, Mode=TwoWay}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<CheckBox IsEnabled="{Binding enabledVar}"
IsChecked="{Binding isChecked, Mode=Twoway}"
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
Imagine there will be 10 items in objectList. Each of the item in objectList will have a variable isChecked which to manipulate the checkbox IsChecked property.
I have another variable in the same viewmodel named enabledVar which to control the ten checkbox IsEnabled property. enabledVar is not part of the objectList but I couldn't get the value. May I know how to handle such case?
Updates:
I've found some new direction but not sure if it helps.
<CheckBox IsEnabled="{Binding enabledVar,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=telerik:RadGridView}}"
but then of course, still failed.
Any helps would be very much appreciated.
if using Ancestor you have to bind to DataContext
<CheckBox IsEnabled="{Binding Path=DataContext.enabledVar,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=telerik:RadGridView}}"
Another aproach could be to use element binding set name
<telerik:RadGridView x:Name="myGrid" ...
And then in the celltemplate bind to it
<CheckBox IsEnabled="{Binding Path=DataContext.enabledVar, ElementName=myGrid}"
Hope this helps.
The code suggested in Update will not work because GridViewDataColumn is not part of the Visual tree, and hence can not access DataContext.
You will have to make use of proxy data binding, like suggested here. You can get more examples by searching "wpf proxy databinding".
I have been looking on the internet for a solution of the problem I have.
Basically I want to select an item on a datagrid and that should update a few textboxes (not a problem) and a combobox (a nightmare)
Please see a code snippet for the combobox (& textboxes) below :
<TextBox Text="{Binding SelectedStudy.Description, Mode=TwoWay}" Width="200" HorizontalAlignment="Left" />
<ComboBox ItemsSource="{Binding ModalityTypes, Mode=TwoWay}" DisplayMemberPath="ModalityTypeCode"
SelectedItem="{Binding ElementName=_studyDataGrid, Path=SelectedItem.Modality, Mode=TwoWay}" SelectedValuePath="Modality" />
<TextBox Text="{Binding SelectedStudy.Duration, Mode=TwoWay}" Width="200" HorizontalAlignment="Left"/>
My Datagrid :
<sdk:DataGrid x:Name="_studyDataGrid"
Grid.Row="1" Grid.ColumnSpan="2"
ItemsSource="{Binding Studies, Mode=OneWay}"
AutoGenerateColumns="False"
IsReadOnly="True"
SelectionMode="Single"
SelectedItem="{Binding SelectedStudy, Mode=TwoWay}"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
Margin=" 5,5,5,5"
>
The above code doesn't work, meaning whenever I click a row in the datagrid, the combobox doesn't get updated.
I also tried:
<ComboBox ItemsSource="{Binding ModalityTypes, Mode=TwoWay}" DisplayMemberPath="ModalityTypeCode"
SelectedValue="{Binding ElementName=_studyDataGrid, Path=SelectedItem.Modality, Mode=TwoWay}" SelectedValuePath="Modality" />
I'm lost, any ideas?
Regards,
Erik
If I understand correctly, you mean you want the selected value of the ComboBox to display dataGrid.
It is enough to ComboBox.SelectedValue =Cint( DataGrid.SelectedItems.Item(0).FieldName) to add.
this Code Add To DataGrid Selection Changed Event.
Good luck