亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WPF 網格列 def auto 始終從右側剪切

WPF 網格列 def auto 始終從右側剪切

C#
倚天杖 2023-07-09 17:27:59
我有一個 WPF 應用程序,其中有一個網格,其中 2 列設置為 * 和自動。問題是,當我減小窗口的大小時,第二列中的子項將從右側而不是左側被剪切。我希望它們從左側剪輯,因為我已將水平對齊設置為右側。有沒有辦法可以剪切左側的第二列元素?<Window x:Class="WpfApp2.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        xmlns:local="clr-namespace:WpfApp2"        mc:Ignorable="d"        Title="MainWindow" Height="450" Width="800" >    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition Width="*" MinWidth="60"/>            <ColumnDefinition Width="auto"/>        </Grid.ColumnDefinitions>        <DockPanel  HorizontalAlignment="Right" Grid.Column="1">            <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />            <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />            <Label x:Name="mLog"/>        </DockPanel>        <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>        <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>    </Grid></Window>
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

 而不是這個:


<Window x:Class="WpfApp2.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800" >

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*" MinWidth="60"/>

            <ColumnDefinition Width="auto"/>

        </Grid.ColumnDefinitions>

        <DockPanel  HorizontalAlignment="Right" Grid.Column="1">

            <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />

            <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />

            <Label x:Name="mLog"/>

        </DockPanel>

        <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>

        <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>

    </Grid>

</Window>

嘗試這個:


<Window x:Class="WpfApp2.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

            xmlns:local="clr-namespace:WpfApp2"

            mc:Ignorable="d"

            Title="MainWindow" Height="450" Width="800" >

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*" MinWidth="60"/>

                <ColumnDefinition Width="auto"/>

            </Grid.ColumnDefinitions>

            <DockPanel  HorizontalAlignment="Right" Grid.Column="1">

                <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />

                <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />

                <Label x:Name="mLog"/>

            </DockPanel>

            <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me"/>

            <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>

        </Grid>

    </Window>

我刪除了最后一個按鈕寬度屬性。


查看完整回答
反對 回復 2023-07-09
?
呼如林

TA貢獻1798條經驗 獲得超3個贊

我猜你正在嘗試制作 3 列布局,兩側各有 1 個按鈕,中間有 1 個內容通道。


因此,您可以嘗試這樣的操作,其中所有內容都位于具有不同 z-index 和水平對齊的同一網格中,現在,當您調整窗口大小時,標簽保持在中間,按鈕“夾在”標簽內容的右側和后面左邊。


<Grid>

    <Button HorizontalAlignment="Left" Content="click me" Width="150" />

    <Button HorizontalAlignment="Right" Content="click me" Width="150" />

    <Label HorizontalAlignment="Center" Content="abcdef" Width="200" Background="White" />

</Grid>


查看完整回答
反對 回復 2023-07-09
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

在 DockPanel 中,HorizontalAlignment 僅當 DockPanel 的寬度大于其所有子項的總寬度時才有用。按鈕和標簽的大小固定為 200 和 150,我相信 WPF 的行為將從左側剪切元素。


第一個棘手的方法是讓 DockPanel 填充兩列,當網格變小時,DockPanel 的一部分將進入第一列的內容下方,給人一種從左側剪切的感覺。


 <Grid>

     <Grid.ColumnDefinitions>

         <ColumnDefinition Width="*" MinWidth="60"/>

         <ColumnDefinition Width="*"/>

     </Grid.ColumnDefinitions>

     <DockPanel HorizontalAlignment="Right" Grid.ColumnSpan="2" LastChildFill="True">

         <Button Click="Button_Click" Content="click me 2" Width="150" DockPanel.Dock="Right" />

         <Label Content="abcdef 2" Width="200" />

         <Label x:Name="mLog" DockPanel.Dock="Right"/>

     </DockPanel>

     <Button Click="Button_Click" Content="click me 1" HorizontalContentAlignment="Stretch"/>

     <Label Content="abcdef 1" HorizontalContentAlignment="Stretch"/>

 </Grid>

另一種正確的方法是使用另一個 Grid 而不是 DockPanel,確保我們讓第一列填充空間,以便與容器一起減小尺寸。


    <Grid HorizontalAlignment="Right" Grid.Column="1">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*"/>

            <ColumnDefinition Width="150"/>

        </Grid.ColumnDefinitions>

        <Button Click="Button_Click" Content="click me" Grid.Column="1" MinWidth="150"/>

        <Label Content="abcdef" Grid.Column="0"/>

        <Label x:Name="mLog"/>

    </Grid>


查看完整回答
反對 回復 2023-07-09
  • 3 回答
  • 0 關注
  • 177 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號