I need to bind the value of the SelectedItem from the datagrid to:
SelectedItem of a combo box on the same page
Property in the viewmodel
In other words: when I select a row in the datagrid the value in the combobox should change and value of the meant above property should be also set to the value of the selected item of the datagrid.
I tried to use multibinding like this:
<DataGrid.SelectedItem>
<MultiBinding Converter="{StaticResource sapConverter}" >
<Binding Path="SelectedSap" Mode="TwoWay"/>
<Binding ElementName="cbSearchCompanyName" Path="SelectedItem" Mode="OneWay"/>
</MultiBinding>
</DataGrid.SelectedItem>
the SelectedSap here is that property, that I want to update. But when I look at the values() in the converter, value(0) corresponding to the SelectedSap is always Nothing and as a result the property doesn't change as I want.
The binding with the combo works fine.
I try to test it without multibinding. I mean, I don't care about the combo, I'm just changing the value of the property. Like this:
<DataGrid.SelectedItem>
<Binding Path="SelectedSap" Mode="TwoWay"/>
</DataGrid.SelectedItem>
everything works fine.
Where is the trick and how should I implement the functionality I need?
Thank you.
I think there is another good way to accomplish your goals:
<DataGrid Name="dgResults" ItemsSource="{Binding Path=DataGridObj}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Maybe your binding is wrong.
If you get your items in the grid, your ItemsSource is fine. Use the SelectedValue and set the SelectedValuePath to the column you want the data from.
Skip the multibinding and set the binding on the combobox to be set to the SelectedValue of the DataGrid.
<DataGrid Name="dgResults" ItemsSource="{Binding Path=DataGridObj}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="ItemNoX"
Š¢hank u a lot! Both your answers gave me a hint. Actually I have to bind three controls together (imagine functionality "search item" - you have a combo "search by item.X", combo "search by item.Y" and a datagrid with items), that's why I was little confused and started with the multibinding. The things are much more easier. Here's my code that now works:
<StackPanel Orientation="Horizontal" Grid.Row="0" >
<Label Content="Search company by name:"/>
<ComboBox MinWidth="200" Width="Auto" Name="cbSearchCompanyName"
ItemsSource="{Binding CompanyList,Mode=TwoWay}"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="CompanyName1"
SelectedValuePath="Sap"
SelectedItem="{Binding Path=SelectedSap, Mode=TwoWay}"
SelectedValue="{Binding Path=SelectedSap.Sap, Mode=TwoWay}"/>
<Label Content="by SAP number:" />
<ComboBox MinWidth="200" Width="Auto" Style="{StaticResource marginStyle}" Name="cbSearchCompanySap"
ItemsSource="{Binding CompanyList,Mode=TwoWay}"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Sap"
SelectedValuePath="Sap"
SelectedItem="{Binding Path=SelectedSap, Mode=TwoWay}"
SelectedValue="{Binding Path=SelectedSap.Sap, Mode=TwoWay}"/>
</StackPanel>
<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<DataGrid x:Name="CompanyList" AutoGenerateColumns="True"
ItemsSource="{Binding CompanyList,Mode=TwoWay}"
MaxWidth="950" Height="300" Margin="0 2 0 0">
<DataGrid.SelectedItem>
<Binding Path="SelectedSap" Mode="TwoWay"/>
</DataGrid.SelectedItem>
</DataGrid>
</ScrollViewer>
Related
Here is a snippet of XAML:
<ComboBox ItemsSource="{Binding UnileverDataSet.Tables, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Margin="5" x:Name="TableNameComboBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TableName}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding UnileverDataSet.Tables[TableNameComboBox.SelectedIndex]}"
UnileverDataSet is a DataSet made up of about 12 DataTables
The idea here is that when the ComboBox value changes, the DataGrid should update based on the index value from the ComboBox.
Is this possible or should I look at another way of doing this?
If I do: UnileverDataSet.Tables[0], then all works and data displays correctly.
You can do element binding with combobox..
Your combox will display list of items in the "UnileverDataSet.Tables" collection. When ever your select a item in Combo box the selected item will bounded to the Datagrid Items source (since we are using element binding)
Here is the sample code
<DataGrid Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding SelectedItem,ElementName=TableNameComboBox}">
I've got a datagrid in WPF that shows a grid of some data. The data is retrieved form a ViewModel, which contains the following properties:
Public ReadOnly Property Devices() As List(Of Device)
Get
Return FDevices
End Get
.
Public ReadOnly Property ClientNetworks() As List(Of network)
Get
Return fnetwork
End Get
End Property
Both properties are filled with data after constructing the view model.
To use the properties in the Datagrid i use the following XAML.
<DataGrid ItemsSource="{Binding Devices}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Customer" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
------------------ <TextBlock Text="{Binding ClientNetwork.Description}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
------------------> <ComboBox ItemsSource="{Binding ClientNetwork}" DisplayMemberPath="Description"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid>
This should show a textbox with the description and a combobox with strings when edited.
Outside of the Datagrid the combobox works fine. I know this is because of the set ItemsSource on the Datagrid but i can't seem to find how to make it work. i've tried several alterations of the combobox code but none have worked so far.
The goal is to make the user be able to edit the cell and have a combobox presented, from which he can select an string, then the corresponding int will be saved in the database.
UPDATE 1
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.ClientNetworks}"
DisplayMemberPath="Description"
SelectedItem="{Binding ClientNetwork}"
/>
This is how i fixed the resting of the datacontext
I found a way to get it done but i am not sure this is the way it should be done
<Window.Resources>
<CollectionViewSource Source="{Binding ClientNetworks}" x:Key="clientnetworks" />
</Window.Resources>
and in the combobox
<ComboBox ItemsSource="{Binding Source={StaticResource clientnetworks}}" DisplayMemberPath="Description" />
I have a ComboBox which lists the contact methods shown below. The ComboBox displays the correct values therefore the ItemSource Binding is working.
What i am trying to archive is that to display the selected contact method on application startup. I tried to bind the selected value to the ComboBox.Text attribute but i can't figure out how to set the filter.
This is my input data:
<Contact ShowsInterest="true">
<Name>Tester</Name>
<Lastname>Test</Lastname>
<ContactMethods>
<ContactMethod Selected="False">Phone</ContactMethod>
<ContactMethod Selected="False">Email</ContactMethod>
<ContactMethod Selected="True">Letter</ContactMethod>
<ContactMethod Selected="False">Mobile</ContactMethod>
</ContactMethods>
</Contact>
This is my ComboBox:
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.Text>
<Binding XPath="Contact/ContactMethods//*[#Selected='true']"/>
</ComboBox.Text>
</ComboBox>
The XPath Expression should do the following: Display the Element under Contact/ContactMethods/ where selected equals true.
EDIT:
Even setting the Text Property directly won't work.
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.Text>
Phone
</ComboBox.Text>
</ComboBox>
I guess i have to use the SelectedValue Property:
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.SelectedValue>
Phone
</ComboBox.SelectedValue>
</ComboBox>
EDIT2:
This is the working solution, thanks to MikroDel
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.SelectedValue>
<Binding XPath="Contact/ContactMethods/ContactMethod[#Selected='True']"/>
</ComboBox.SelectedValue>
</ComboBox>
This is the correct one :
<Binding XPath="Contact/ContactMethods/ContactMethod[#Selected='True']"/>
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.
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>