Enable a button on a selected item

A few days ago I wrote a post about how to use the generic property setter in order to set IsEnabled on a button. I got the question how to do it in 9.1.2. Unfortunately the M3List does not expose the CurrentItem property in 9.1.2 so for M3Lists I have no solution. But for lists using the M3 APIS (MIListPanel) and the DataListPanel you have the option of checking the SelectedValue of the list and use a converter that will change a not null value to true.

My example has a list of Countries and a button that will be disabled unless a row in the list is selected.

Here is the xaml:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:util="clr-namespace:Mango.UI.Utils;assembly=Mango.UI" xmlns:mashup="clr-namespace:Mango.UI.Services.Mashup;assembly=Mango.UI" xmlns:m3="clr-namespace:MForms.Mashup;assembly=MForms">
  <Grid.Resources>
   <util:IsNotNullOrEmptyConverter x:Key="notNullConverter" />
   <!-- the inversion of the first converter-->
   <util:IsNullOrEmptyConverter x:Key="isNullConverter" />
  </Grid.Resources>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="1*" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="1*" />
  </Grid.ColumnDefinitions>
  <m3:MIListPanel Name="CountryList" Margin="8">
  <m3:MIListPanel.DataSource>
    <m3:MIDataSource Program="CRS045MI" Transaction="LstCountry" InputFields="" />
  </m3:MIListPanel.DataSource>

  <m3:MIListPanel.Events>
    <mashup:Events>
       <mashup:Event SourceEventName="Startup">
        </mashup:Event>
      </mashup:Events>
  </m3:MIListPanel.Events>

  <ListView Name="listViewCountries" HorizontalAlignment="Left" ItemsSource="{Binding Items}" Width="300" Height="300" Style="{DynamicResource styleListView}" ItemContainerStyle="{DynamicResource styleListViewItem}">
    <ListView.View>
      <GridView ColumnHeaderContainerStyle="{DynamicResource styleGridViewColumnHeader}">
        <GridView.Columns>
          <GridViewColumn Header="Country" DisplayMemberBinding="{Binding [CSCD]}" />
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding [TX15]}" />
          <GridViewColumn Header="Currency" DisplayMemberBinding="{Binding [CUCD]}" />
        </GridView.Columns>
      </GridView>
    </ListView.View>
  </ListView>
  </m3:MIListPanel>
    <Button Content="My Button" HorizontalAlignment="Left" Grid.Row="1" Margin="8,16,8,8" IsEnabled="{Binding Path=SelectedItem, ElementName=listViewCountries, Converter={StaticResource notNullConverter}}" />
</Grid>

1 thought on “Enable a button on a selected item

Comments are closed.