Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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# 列表框内网格中的复选框水平对齐不工作_C#_Wpf_Xaml_Checkbox_Listbox - Fatal编程技术网

C# 列表框内网格中的复选框水平对齐不工作

C# 列表框内网格中的复选框水平对齐不工作,c#,wpf,xaml,checkbox,listbox,C#,Wpf,Xaml,Checkbox,Listbox,我有一个DataGrid,其中我使用rowdailstemplate来显示所选DataGrid项的详细信息。显示的信息类似于“子项”,因此我使用一个带有文本块的列表框和两个复选框的控件来显示所有信息。 使用网格定位控件,但我无法将复选框控件居中放置在网格中。它们总是左对齐。 我读过几个关于Stackoverflow的问题,都有类似的问题,但都不适合我。如何将两个复选框控件置于分配给它们的网格列的中心?这是我的XAML代码: <DataGrid.RowDetailsTemplate>

我有一个
DataGrid
,其中我使用
rowdailstemplate
来显示所选
DataGrid
项的详细信息。显示的信息类似于“子项”,因此我使用一个带有
文本块的
列表框和两个
复选框的
控件来显示所有信息。
使用
网格
定位控件,但我无法将
复选框
控件居中放置在
网格
中。它们总是左对齐。
我读过几个关于Stackoverflow的问题,都有类似的问题,但都不适合我。如何将两个
复选框
控件置于分配给它们的
网格列
的中心?这是我的XAML代码:

<DataGrid.RowDetailsTemplate>
<DataTemplate>
    <ListBox Name="lbDependencyDetails" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Path=DependencyView, UpdateSourceTrigger=PropertyChanged}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="55" />
                        <ColumnDefinition Width="55" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" 
                               Grid.ColumnSpan="2" 
                               Text="{Binding Path=Dependency.SoftwareName}" 
                               Margin="{Binding Path=Margin}" 
                               HorizontalAlignment="Left" />

                    <!--<GridSplitter HorizontalAlignment="Right" Grid.Column="1" Width="2" />-->

                    <CheckBox Grid.Column="2" 
                              IsChecked="{Binding Path=Dependency.IsMarkedForReinstall, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                              HorizontalAlignment="Center" 
                              IsEnabled="False" 
                              Width="55" />
                    <CheckBox Grid.Column="3" 
                              IsChecked="{Binding Path=Dependency.IsMarkedForRepair, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                              HorizontalAlignment="Center" 
                              IsEnabled="False" 
                              Width="55" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DataTemplate>


由于您的
列定义
已将列的宽度设置为55,并且渲染的宽度复选框也设置为55,因此它将占用整个空间。因此,可以将
列定义更改为*或从复选框中删除宽度

<DataGrid.RowDetailsTemplate>
<DataTemplate>
    <ListBox Name="lbDependencyDetails" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Path=DependencyView, UpdateSourceTrigger=PropertyChanged}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" 
                               Grid.ColumnSpan="2" 
                               Text="{Binding Path=Dependency.SoftwareName}" 
                               Margin="{Binding Path=Margin}" 
                               HorizontalAlignment="Left" />

                    <!--<GridSplitter HorizontalAlignment="Right" Grid.Column="1" Width="2" />-->

                    <CheckBox Grid.Column="2" 
                              IsChecked="{Binding Path=Dependency.IsMarkedForReinstall, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                              HorizontalAlignment="Center" 
                              IsEnabled="False" 
                              Width="55" />
                    <CheckBox Grid.Column="3" 
                              IsChecked="{Binding Path=Dependency.IsMarkedForRepair, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                              HorizontalAlignment="Center" 
                              IsEnabled="False" 
                              Width="55" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DataTemplate>


网格。列
仅用于指定总宽度,因此,当您将整个宽度指定为复选框的宽度时,在加载UI时,它占用了整个空间。

就是这样!谢谢!你能帮我更好地理解这个问题吗?如果我将复选框宽度设置为55,它占用的完整空间是多少?网格,柱?