WPF में आयत का आकार ग्रिड में कैसे सेट करें


मुझे आयत का आकार स्वचालित रूप से ग्रिड का आवश्यक आकार लेने की आवश्यकता है।
कृपया कोई विचार?

मैंने क्या प्रयास किया है:

एक्सएमएल
<Grid x:Name="GridRecGrip" Width="10" Height="20">
    <Rectangle x:Name="RecGrip"
               Fill="Red"
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"
               Width="{Binding Width, Source=GridRecGrip}" 
               Height="{Binding Height, Source=GridRecGrip}">
    </Rectangle>
</Grid>

समाधान 1

आपको इसका आकार निर्धारित करने की आवश्यकता नहीं है Rectangle यदि आप भरना चाहते हैं Grid क्षेत्र।

एक्सएमएल
<Grid Width="10" Height="20">
    <Rectangle Fill="Red"></Rectangle>
</Grid>

अब, यदि आप हटा दें Width और Height की GridGrid मूल स्थान को भर देगा और आयत ग्रिड को भर देगा। यदि ग्रिड का पैरेंट फॉर्म है तो फॉर्म का बैकग्राउंड दोनों से भरा जाएगा Grid और यह Rectangle साथ Red भरना।

एक्सएमएल
<Window x:Class="WpfGridRect.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Rectangle Fill="Red"></Rectangle>
    </Grid>
</Window>

अधिकांश, सभी नहीं, उपलब्ध क्षेत्र को भर देंगे। StackPanel यह एक अपवाद है जब तक कि आप इसे बताएं नहीं।

अद्यतन

पूछे गए प्रश्न में कहीं भी यह निर्धारित नहीं किया गया है कि मूल एक डेटाग्रिडसेल था। हालाँकि, वही सिद्धांत लागू होता है:

एक्सएमएल
<DataGridTemplateColumn Width="100">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Rectangle Fill="DeepSkyBlue"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Rectangle भर देगा DataGridTemplateColumn.

コメント

タイトルとURLをコピーしました