Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 视口宽度略宽于真实视图窗口WPF_C#_Wpf_Xaml_Binding - Fatal编程技术网

C# 视口宽度略宽于真实视图窗口WPF

C# 视口宽度略宽于真实视图窗口WPF,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我准备了自己的DataTemplate来显示我自己的项目类对象。要设置宽度,我使用绑定到ScrollViewer的ViewportWidth。这就是为什么: <DataTemplate x:Key="MyItemTemplate"> <Grid Margin="5" HorizontalAlignment="Stretch" Width="{Binding ViewportWidth, RelativeSource={Relative

我准备了自己的
DataTemplate
来显示我自己的项目类对象。要设置宽度,我使用绑定到
ScrollViewer
ViewportWidth
。这就是为什么:

<DataTemplate x:Key="MyItemTemplate">
        <Grid Margin="5" HorizontalAlignment="Stretch"
              Width="{Binding ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollViewer}}}">
            ...
        </Grid>
    </DataTemplate>

ListView为每个项目生成的项目容器元素的默认样式包含一些填充。这意味着,渲染项的宽度通常略小于视口宽度

为了不让项目穿过视口的右边框,可以将项目容器的填充设置为零。这可以通过项目容器的简单样式轻松完成。因为您已经提供了一个项目容器样式,所以可以让它设置padding属性

但是,这可能没有必要。若要跨视图端口的宽度拉伸项目内容,无需将项目模板中的Grid.width属性绑定到ListView.ViewPortWidth。这样做更简单:可以通过相应地设置项目容器的
HorizontalContentAlignment
来指示项目容器水平拉伸项目内容。当然,这也可以通过项目容器样式完成

下面的示例样式(基于问题中给出的样式)演示了如何设置
HorizontalContentAlignment
Padding
属性。如果要在项目和ListView边框之间保留默认的填充,只需从我的示例中省略填充设置器:

<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">

    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="Padding" Value="0" />

    <Style.Triggers>
        <!-- setting up triggers for alternate background colors -->
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#FF4C85FF"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="2">
            <Setter Property="Background" Value="#FFFF8C7C"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>


嗯,我看你有很多错误,我想:Premoving
Margin=“5”
没有解决问题:/Hmm,不知道。如果您可以添加一个屏幕截图,显示问题的具体表现。这可能有助于了解正在发生的事情。您的问题中的XAML片段说明不了多少…另外,请显示您正在使用的ItemContainerStyle(alternatingListViewItemStyle)。嗯。。。我从脑后不知道,但可能是ScrollViewer或ItemsPanel在内部某处使用了一些边框/填充/边距。但我现在还不知道。当我明天坐在工作站上时,我需要更仔细地看看ListView中发生了什么。。。
<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <!-- setting up triggers for alternate background colors -->
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#FF4C85FF"></Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                <Setter Property="Background" Value="#FFFF8C7C"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">

    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="Padding" Value="0" />

    <Style.Triggers>
        <!-- setting up triggers for alternate background colors -->
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#FF4C85FF"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="2">
            <Setter Property="Background" Value="#FFFF8C7C"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>