XAML 中的强制绑定更新

发布于 2024-09-19 16:46:28 字数 4694 浏览 3 评论 0原文

我有一个带有列表框的视图和几个绑定到列表框中显示的对象的属性的文本框。打开时,列表框填充了数据,并且我在样式中有以下内容,以确保在有项目但未选择任何项目时选择第一个项目。

    <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-09-26 16:46:28

下面是 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文