我已经和这个xaml战斗了好几个小时了。应该很简单。我有一个通用的 WindowDialog (Billing.WindowDialog),其中 ContentPresenter 显示UserControl(NovaLibraries.Views.DxLibraryView)。 UserControl 本身有一个 ListView (在行= 0),后面是其他控件。
好消息:当WindowDialog呈现UserControl时,它会正确显示UserControl到显示屏的大小并最小化 WindowDialog(或调整它的大小)确实正确地改变了UserControl的大小并正确缩小了ListView。
问题(坏消息):当过滤器放在ListView中包含的列表上时,ListView的项目较少,由于缺少项目而缩小,整个UserControl缩小。当列表很大而一百个项目然后缩小到4个项目时,这会变得非常分散注意力,因为这会导致整个UserControl缩小。
我需要什么:我最初需要UserControl使用所有可用空间最大化到使用ListView的屏幕大小。 UserControl将根据其父WindowDialog缩小(或放大) - 而不是根据ListView的内容。
感谢您阅读所有这些内容。非常感谢任何帮助。
WindowDialog:
<Window x:Class="Billing.WindowDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="SingleBorderWindow"
WindowStartupLocation="CenterOwner"
Title="{Binding DialogTitle}">
<ContentPresenter x:Name="DialogPresenter" Content="{Binding .}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Window>
using System.Windows;
using Client;
namespace Billing
{
/// <summary>
/// Interaction logic for WindowDialog.xaml
/// </summary>
public partial class WindowDialog : Window, IWindowDialog
{
public WindowDialog()
{
InitializeComponent();
}
}
}
显示UserControl的C#代码:
var dialog = new WindowDialog
{
Title = "ICD Library",
ShowInTaskbar = false,
Topmost = false
};
DialogService dialogService = new DialogService();
dialogService.ShowDialog(
dialog,
new DxLibraryViewModel(null, null)
);
// Opens a window and returns only when the newly opened window is closed.
public void ShowDialog<TDialogViewModel>(IWindowDialog windowdialog, TDialogViewModel viewModel,
Action<TDialogViewModel> onDialogClose, bool sizetocontent = false)
{
// Assign the viewmodel as the DataContext of the WindowDialog.xaml
windowdialog.DataContext = viewModel;
// Set window size.
SizeToContent SizeToContent = SizeToContent.Manual;
WindowState WindowState = WindowState.Maximized;
if (sizetocontent)
{
SizeToContent = SizeToContent.WidthAndHeight;
WindowState = WindowState.Normal;
}
// The WindowDialog is the mainwindow.
(windowdialog as WindowDialog).WindowState = WindowState;
(windowdialog as WindowDialog).SizeToContent = SizeToContent;
// Open MainWindow in modal fashion (Returns only when the newly opened window is closed).
// Implicict DataTemplate in App.XAML used to display usercontrol.
windowdialog.ShowDialog();
}
public void ShowDialog<TDialogViewModel>(IWindowDialog mainwindow, TDialogViewModel viewModel, bool sizetocontent = false)
{
ShowDialog(mainwindow, viewModel, null, sizetocontent);
}
UserControl是:
<UserControl x:Class="NovaLibraries.Views.DxLibraryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:c="clr-namespace:NovaLibraries.Converters"
mc:Ignorable="d"
BorderBrush="Black" BorderThickness="2" Background="Beige" >
<UserControl.Resources>
<!--Display a checkmark without the surrounding box as an image-->
<c:CheckMarkConverter x:Key="mycheckmark" />
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}">
<Style.Resources>
<!--ORANGE-->
<LinearGradientBrush x:Key="OrangeBrush" StartPoint="0.5,0" EndPoint="0.5,1" >
<GradientStop Offset="0.1" Color="#AA00CC00" />
<GradientStop Offset="0.8" Color="Orange" />
</LinearGradientBrush>
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=chronic}" Value="true">
<Setter Property="Background" Value="{StaticResource OrangeBrush}" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100*" />
<RowDefinition Height="275" />
</Grid.RowDefinitions>
<ListView Grid.Row="0"
ItemContainerStyle="{StaticResource ItemContStyle}"
ItemsSource="{Binding DxView.View}"
SelectedItem="{Binding SelectedItem}"
IsEnabled="{Binding CanSelect}"
Width="781" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=rank}" Header="Rank" Width="50" />
<GridViewColumn DisplayMemberBinding="{Binding Path=code}" Header="ICD-9" Width="50" />
<GridViewColumn DisplayMemberBinding="{Binding Path=cdesc}" Header="Diagnosis" Width="550" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
答案 0 :(得分:0)
删除
HorizontalAlignment="Center" VerticalAlignment="Center"
来自ContentPresenter的,ListView及其父UserControl按预期运行。