Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I have a C# WPF application. It has a window as image below:
The Image[^]

The list view has 3 columns. the first and the second are implemented:

XML
<Window x:Class="InfoManager.Presentation_Layer.WindowProperty"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowProperty" Height="278" Width="480">
    <Grid>
        <ListView ItemsSource="{Binding PropertyCollection}"  Margin="12,86,12,12" Name="listView1">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="140"/>
                    <GridViewColumn Header="Type" Width="140">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Path=PropertyCollection}" Text="{Binding Path=PropertyTypeText}"  DisplayMemberPath="Value" SelectedValuePath="Key" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}" Width="140"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Button" Height="23" Margin="0,26,37,0" Name="buttonAdd" VerticalAlignment="Top" HorizontalAlignment="Right" Width="53" Click="buttonAdd_Click" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,30,0,0" Name="textBlock1" Text="Add new property:" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="116,27,96,0" Name="textBoxNewProperty" VerticalAlignment="Top" />
    </Grid>
</Window>



But the problem is that the 3rd column depends on the value of the 2nd column and in XAML we cannot have if command.
what should I do?
Posted

1 solution

You would need to bind the combobox's SelectedItem to a property in the codebehind/viewmodel that returns an object of the same type as the collection that is the source of the combobox. Then you bind the third column to that property.

So say your combobox column is bound to a collection of customers, then you would have another property in the viewmodel/codebehind called SelectedCustomer. You would bind the ComboBox columns SelectedItem to the SelectedCustomer property, setting IsSynchronisedWithCurrentItem to true, and also bind the third column to that SelectedCustomer property. Thus when the combo selection is changed, it will trigger the SelectedCustomer to change which would update the third column.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900