XAML 中的强制绑定更新
我有一个带有列表框的视图和几个绑定到列表框中显示的对象的属性的文本框。打开时,列表框填充了数据,并且我在样式中有以下内容,以确保在有项目但未选择任何项目时选择第一个项目。
<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 属性的文本框(通过其父网格数据上下文)似乎也没有收到通知。
任何人都知道强制更新的方法(如果可能的话,使用 XAML)。目前,绑定如下所示:
<TextBox Text="{Binding Weight, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
编辑
下面是显示使用 PackageList SelectedItem 作为其数据上下文的网格 PackageDetailsGrid 的 XAML:
<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>
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}" />
Edit
Below is the XAML showing the grid PackageDetailsGrid using the PackageList SelectedItem as its 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是 TextBox 的一些 xaml,其 Text 属性绑定到列表视图的 SelectedItem 属性。它包括用于在未选择任何内容时自动选择第一项的代码。
这是您正在寻找的解决方案吗?如果没有,我需要更多信息,即您正在处理的所有相关代码。
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.