簡體   English   中英

如何獲得滾動查看器的可滾動高度以在交互請求中更新

[英]How to get a scrollviewer's scrollable height to update in an interaction request

編輯:我發現實際上是我的項目控件中的項目演示者在滾動查看器中而不是在滾動查看器中沒有正確更新。 我添加了一個答案來反映這一點。

我為自定義視圖交互請求進行了簡單的設置。 該視圖包含一個滾動查看器,但是如果滾動查看器中的項目控件具有項目源更新,則滾動查看器的可滾動高度不會更新。 相關代碼如下。

確認模型:

public class ProfileImportConfirmation : Confirmation
{
    public ObservableCollection<ProfileAcceptPair> PossibleProfiles { get; set; } = new ObservableCollection<ProfileAcceptPair>();
    public ObservableCollection<Profile> ConfirmedProfiles { get; set; } = new ObservableCollection<Profile>();
}

視圖模型:

public class ProfileImportPopupViewModel : BindableBase, IInteractionRequestAware
{
    ProfileImportConfirmation _profileImportConfirmation;

    public InteractionRequest<Confirmation> YesNoConfirmationInteractionRequest { get; }

    public DelegateCommand AcceptCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }

    public ProfileImportPopupViewModel()
    {
        AcceptCommand = new DelegateCommand(Accept);
        CancelCommand = new DelegateCommand(Cancel);
        YesNoConfirmationInteractionRequest = new InteractionRequest<Confirmation>();
    }

    public INotification Notification
    {
        get { return _profileImportConfirmation; }
        set
        {
            if (value is ProfileImportConfirmation confirmation)
            {
                _profileImportConfirmation = confirmation;
                OnPropertyChanged(nameof(Notification));
            }
        }
    }

    public Action FinishInteraction { get; set; }

    void Cancel()
    {
        _profileImportConfirmation.Confirmed = false;
        FinishInteraction();
    }

    void Accept()
    {
        _profileImportConfirmation.Confirmed = true;
        _profileImportConfirmation.ConfirmedProfiles.Clear();
        _profileImportConfirmation.ConfirmedProfiles.AddRange(_profileImportConfirmation.PossibleProfiles.Where(p => p.Accepted).Select(p => p.Profile).ToList());
        if (_profileImportConfirmation.ConfirmedProfiles.Any(p => p.IsRootProfile))
            YesNoConfirmationInteractionRequest.Raise(
                new Confirmation
                {
                    Title = DisplayStrings.AreYouSureLabel,
                    Content = "Proceed?"
                },
                confirmed => FinishInteraction());
        else
        {
            FinishInteraction();
        }
    }
}

視圖:

<UserControl 
         MaxHeight="500"
         MinWidth="400"
         d:DataContext="{d:DesignInstance Type=viewModels:ProfileImportPopupViewModel, IsDesignTimeCreatable=False}"
         Loaded="ProfileImportPopup_OnLoaded">

<i:Interaction.Triggers>
    <mvvm:InteractionRequestTrigger SourceObject="{Binding YesNoConfirmationInteractionRequest, Mode=OneWay}">
        <mvvm:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStyle="{StaticResource PopupWindow}" WindowStartupLocation="CenterOwner">
            <mvvm:PopupWindowAction.WindowContent>
                <popups:YesNoConfirmationPopup />
            </mvvm:PopupWindowAction.WindowContent>
        </mvvm:PopupWindowAction>
    </mvvm:InteractionRequestTrigger>
</i:Interaction.Triggers>

<Grid Margin="30, 0, 30, 30">
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/> 
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Label Grid.ColumnSpan="2" Content="{Binding Notification.Title}" HorizontalAlignment="Left" FontFamily="{StaticResource 'Brandon Grotesque Bold'}" FontSize="{StaticResource LargeFontSize}"/>

    <Label Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding Notification.Content}" HorizontalAlignment="Center" VerticalContentAlignment="Center" FontFamily="{StaticResource 'Brandon Grotesque Bold'}" FontSize="{StaticResource LargeFontSize}"/>

    <ScrollViewer x:Name="aoeu" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" CanContentScroll="True" VerticalScrollBarVisibility="Auto">
        <ItemsControl ItemsSource="{Binding Notification.PossibleProfiles}" Margin="0, 0, 30, 0">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type models:ProfileAcceptPair}">
                    <CheckBox Style="{StaticResource RightAlignedCheckBox}" Content="{Binding Name}" IsChecked="{Binding Accepted}" HorizontalContentAlignment="Right"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

    <Button Grid.Row="3" Grid.Column="0" HorizontalAlignment="Center" Content="{x:Static resources:DisplayStrings.CancelButton}" Style="{StaticResource ModalWindowButton}" Command="{Binding CancelCommand}" Margin="0" VerticalAlignment="Center"/>
    <Button Grid.Row="3" Grid.Column="1" Content="{x:Static resources:DisplayStrings.OKButton}" Style="{StaticResource ModalWindowButton}" Command="{Binding AcceptCommand}" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>

看來item來源正在更新,我可以看到隱藏在滾動查看器下方的新item元素,但無法向下滾動。

如何獲取可滾動的高度以進行更新?

問題不在於滾動查看器。 它是滾動查看器中項目控件中的項目演示者。 它不是在更改項目時更新高度。

我的解決方案不是理想的,但是有效。 我在后面的代碼中為用戶控件添加了已加載的事件處理程序。 然后,我將項目控件命名為,並使用該控件找到項目展示者子級,並稱為無效度量。

void Popup_OnLoaded(object sender, RoutedEventArgs e)
        {
            var itemsPresenter = (ItemsPresenter) FindChild(MyItemsControl, typeof(ItemsPresenter));
            itemsPresenter.InvalidateMeasure();
        }

public DependencyObject FindChild(DependencyObject o, Type childType)
            {
                DependencyObject foundChild = null;
                if (o != null)
                {
                    var childrenCount = VisualTreeHelper.GetChildrenCount(o);
                    for (var i = 0; i < childrenCount; i++)
                    {
                        var child = VisualTreeHelper.GetChild(o, i);
                        if (child.GetType() != childType)
                        {
                            foundChild = FindChild(child, childType);
                        }
                        else
                        {
                            foundChild = child;
                            break;
                        }
                    }
                }
                return foundChild;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM