1

I would like to enable or disable a field when a specific value in the combobox is selected:

In my xaml, I have a ListBox (ListToTransfert)wich is filled by dragging items from another Listbox (ListViewContents).

The ListToTransfert got 3 fields: Name, Gapand Time. The field Namecannot be edited while the 2 others can be.

When the combobox has its value changed to Gapor to Time, I want the appropriate column to be enabled while the other one is disabled.

Here is the code of my XAML:

     <!--    ListViewContents  -->

        <Label Content="Contents" Background="White" HorizontalContentAlignment="Center" HorizontalAlignment="Left" Margin="11,16,0,0" VerticalAlignment="Top" Width="400" Height="31"/>
        <ListView Background="#bdc3c7" x:Name="ListViewContents" SelectionMode="Single" Margin="11,51.826,0,11" HorizontalAlignment="Left" Width="400">
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="Name" Width="350" DisplayMemberBinding="{Binding Name}"/>
                </GridView>
            </ListView.View>

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <command:EventToCommand Command="{Binding TransferContent}"  
                        CommandParameter="{Binding SelectedItem, ElementName=ListViewContents}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <Label Content="Label" Height="100" Width="100"/>
        </ListView>

     <!--    ListToTransfert  -->
     <TextBox Style="{StaticResource {x:Static ToolBar.TextBoxStyleKey}}" 
                     HorizontalContentAlignment="Center" 
                     HorizontalAlignment="Left" Margin="851,16,0,0" 
                     VerticalAlignment="Top" Width="400" Height="31" Text="{Binding groupName}" />

            <ListView Background="#bdc3c7" x:Name="ListToTransfert" ItemsSource="{Binding ContentsToTransfer}" HorizontalAlignment="Right" Width="400" Margin="0,51.826,21,11">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Amount" >
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition SharedSizeGroup="A" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Foreground="Black" Background="AliceBlue" Text="{Binding}"/>
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>

                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition SharedSizeGroup="A" />
                                        </Grid.ColumnDefinitions>

                                        <TextBlock Foreground="Black" Background="AliceBlue" HorizontalAlignment="Right" Text="{Binding Path=Name}"/>
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                <GridViewColumn Header="Gap"></GridViewColumn>
                <GridViewColumn Header="Time"></GridViewColumn>
                    </GridView>
                </ListView.View>

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <command:EventToCommand Command="{Binding DeleteContent}"  
                            CommandParameter="{Binding SelectedItem, ElementName=ListToTransfert}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </ListView>

My combobox is not yet created since i'm not sure if there is a solution for this problem, tho there is always one.

Can you propose me a XAML solution to do that?

DueSouth
  • 97
  • 3
  • 14

1 Answers1

1

This can be done either via a group of RadioButtons or a single ComboBox.

RadioButton

If you don't need to control this behavior from the view model, you can use the following :

<RadioButton x:Name="TimeRadioButton" Content="Time" />

<GridViewColumn Header="Gap">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock IsEnabled="{Binding IsChecked, ElementName=GapRadioButton}"/>
            </Grid>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Alternatively, you can add EnableGapColumn/EnableTimeColumn on your view model. However, it would be better if you create an enum to define this behavior :

public enum TransfertViewType
{
    Gap, Time
}

And change your bindings with EnumToBooleanConverter:

<!-- put this in a resource -->
<converter:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />

<RadioButton Content="Time" IsChecked="{Binding ViewType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:YourViewModel.TransfertViewType}}"/>

<TextBlock IsEnabled="{Binding ViewType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:YourViewModel.TransfertViewType}}"/>

ComboBox

It would pretty much the same as the second part of RadioButton section, except that you will need to create an IEnumerable<TransfertViewType> as the ItemsSource for the ComboBox. Or, an IDictionary<TransfertViewType, string> if you like to customize the description a little bit.

<!-- for ienumerable -->
<ComboBox ItemsSource="{Binding TransfertViewTypes}"
          SelectedValue="{Binding ViewType}" />

<!-- for idictionary -->
<ComboBox ItemsSource="{Binding TransfertViewTypes}"
          DisplayMemberPath="Value" SelectedValuePath="Key"
          SelectedValue="{Binding ViewType}" />
Community
  • 1
  • 1
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44