我有一个带有列表框的视图和几个绑定到列表框中显示的对象属性的文本框。在打开时,列表框中填充了数据,并且我确保样式中的以下内容比有选项时没有选择任何项目时选择第1项。
<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>
这很有效。列表中的第一项始终在填充列表时被选中。
不幸的是,即使选择了第一个项目,绑定到selectedItems属性的文本框(通过其父网格datacontext)似乎也没有收到通知。
任何人都知道强制他们更新的方法(如果可能的话,在XAML中)。目前,绑定看起来如此:
<TextBox Text="{Binding Weight, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
非常感谢任何帮助。
科里
=================编辑====================
下面是使用PackageList SelectedItem显示网格PackageDetailsGrid的XAML,因为它是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>
答案 0 :(得分:0)
这是TextBox的一些xaml,其Text属性绑定到列表视图的SelectedItem属性。它包含用于在没有选择任何内容时自动选择第一个项目的代码。
这是您正在寻找的解决方案吗?如果没有,我需要更多信息,即您正在处理的所有相关代码。
<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>