views:

54

answers:

1

I have a View with a Listbox and several textboxes bound to properties of the objects displayed in the listbox. On opening, the listbox is populated with data, and I have the following in the style to ensure than when there are items and nothing is selected, to select the 1st item.

    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="SelectedItem" Value="{x:Null}"/>
                <Condition Property="HasItems" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="SelectedIndex" Value="0"/>
        </MultiTrigger>
    </Style.Triggers>

This works. The first item in the list is always selected when the list gets populated.

Unfortunately, even though the first item is selected, the textboxes that are bound to the selectedItems properties (via their parent grids datacontext) do not seem to receive notification.

Anyone know of a way to force them to update (in XAML if possible). Currently, bindings look thusly:

<TextBox Text="{Binding Weight, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

Any help would be greatly appreciated.

Cory

=================Edit====================

Below is the XAML showing the grid PackageDetailsGrid using the PackageList SelectedItem as it's datacontext:

<StackPanel Orientation="Vertical" d:LayoutOverrides="Height">
    <TextBlock Text="Packages" Style="{DynamicResource TitleText}"/>
    <ListBox x:Name="PackageList" Style="{StaticResource SnazzyList}" FocusVisualStyle="{x:Null}" Margin="0" ItemsSource="{Binding Source={StaticResource Packages}}" HorizontalContentAlignment="Stretch" Height="132.5" Background="#18000000">
    </ListBox>
    <Grid Margin="0,0,8,0">
        <Button Content="Add" Margin="20,0,0,0" Width="87" HorizontalAlignment="Left" Style="{DynamicResource ClearButton}" Command="{Binding AddPackageCommand}" Visibility="{Binding ShipmentRecord.TransitStatus, Converter={StaticResource ShippedToVisibilityConverter}}"/>
        <Button Content="Delete" Margin="0,0,20,0" Style="{DynamicResource ClearButton}" HorizontalAlignment="Right" Width="87" Height="21.4666666666667" Command="{Binding DeletePackageCommand}" CommandParameter="{Binding SelectedItem, ElementName=PackageList, Mode=Default}" Visibility="{Binding ShipmentRecord.TransitStatus, Converter={StaticResource ShippedToVisibilityConverter}}"/>
    </Grid>
</StackPanel>
<Grid x:Name="PackageDetailsGrid" Margin="0" Grid.Column="1" DataContext="{Binding Items.CurrentItem, ElementName=PackageList, Mode=Default}">
    <StackPanel Margin="0">
        <Grid Margin="0,0,0,8">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="40*"/>
                <ColumnDefinition Width="60*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="Dimensions" Foreground="White" FontWeight="Bold"/>
            <StackPanel Grid.Column="1" Orientation="Horizontal" d:LayoutOverrides="Height">
                <TextBox Text="{Binding Height, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="48" TextWrapping="Wrap" Margin="0" HorizontalAlignment="Left" IsEnabled="{Binding CanEnterPackageDetails}">
                    <i:Interaction.Behaviors>
                        <local:SelectAllOnFocusTextboxBehavior/>
                    </i:Interaction.Behaviors>
                </TextBox>
                <TextBlock Text="X" Style="{DynamicResource XTextBlockStyle}"/>
                <TextBox Text="{Binding Width, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="48" TextWrapping="Wrap" Margin="0" HorizontalAlignment="Left" IsEnabled="{Binding CanEnterPackageDetails}">
                    <i:Interaction.Behaviors>
                        <local:SelectAllOnFocusTextboxBehavior/>
                    </i:Interaction.Behaviors>
                </TextBox>
                <TextBlock Text="X" Style="{DynamicResource XTextBlockStyle}"/>
                <TextBox Text="{Binding Length, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="48" TextWrapping="Wrap" Margin="0" HorizontalAlignment="Left" IsEnabled="{Binding CanEnterPackageDetails}">
                    <i:Interaction.Behaviors>
                        <local:SelectAllOnFocusTextboxBehavior/>
                    </i:Interaction.Behaviors>
                </TextBox>
            </StackPanel>
        </Grid>
    </StackPanel>
</Grid>
A: 

Here is some xaml for a TextBox whose Text property is bound to the SelectedItem property of the list view. It includes your code for automatically selecting the first item when there is nothing selected.

Is this the kind of solution you're looking for? If not, I need more info i.e. all of the relevant code you're working on.

<StackPanel>
    <TextBox Text="{Binding SelectedItem, ElementName=MyListView}" />

    <ListView x:Name="MyListView">
        <ListView.Style>
            <Style TargetType="ListView">
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="SelectedItem" Value="{x:Null}"/>
                            <Condition Property="HasItems" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="SelectedIndex" Value="0"/>
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Style>

        <ListView.Items>
            <System:String>hello</System:String>
            <System:String>world</System:String>
        </ListView.Items>
    </ListView>
</StackPanel>
nzhenry
I am certain that would work, but these are data objects that are retrieved in a separate thread as the View is loading. As such, often the objects are not loaded when the listBox is instanced. It looks like because of the loading being delayed, there is some kind of notification that is not happening.
OffApps Cory