Silverlight 网格未填满整个 space

Silverlight grid not filling entire space

我在 wcf 中有内容对象。 我尝试存储在内容 属性 网格中,但它没有填满整个长度。

函数return网格:

private Grid ChangeContentObject()
        {
            Grid g = new Grid();
            g.Background = new SolidColorBrush(Colors.Red);

            g.HorizontalAlignment = HorizontalAlignment.Stretch;
            g.VerticalAlignment = VerticalAlignment.Stretch;

            ColumnDefinition columnDefinitionForPath = new ColumnDefinition();
            columnDefinitionForPath.Width = new GridLength(4,GridUnitType.Star);

            ColumnDefinition columnDefinitionForEmpty = new ColumnDefinition();
            columnDefinitionForEmpty.Width = new GridLength(6, GridUnitType.Star);

            g.ColumnDefinitions.Add(columnDefinitionForPath);
            g.ColumnDefinitions.Add(columnDefinitionForEmpty);

            WindowsShapes.Path p = new WindowsShapes.Path();
            p.Stroke = new SolidColorBrush(Colors.Brown);
            p.StrokeThickness = 2;

            p.HorizontalAlignment = HorizontalAlignment.Stretch;

            var b = new Binding
            {
                Source = "M50,0 L0,0 L0,50 L50,50"
            };
            BindingOperations.SetBinding(p, WindowsShapes.Path.DataProperty, b);

            p.Stretch = Stretch.Fill;

            g.Children.Add(p);
            Grid.SetColumn(p, 0);

            return g;
        }

设置内容代码:

objectVisual.Content = ChangeContentObject();

objectVisual 属性:

objectVisual.VerticalAlignment = Stretch
objectVisual.VerticalAlignment
objectVisual.Width = 100
objectVisual.Height = 50

我得到下一个结果:

为什么网格没有填满整个长度?

您的 Grid 有 2 列。第二列是空的,因此折叠起来,所以您看不到它。将某些东西绑定到它可以解决 "problem"。另外,为这个演示修改了你的 Path 数据,因为你的数据不好。看一看:

private Grid ChangeContentObject()
{
    Grid g = new Grid();
    g.Background = new SolidColorBrush(Colors.Red);

    g.HorizontalAlignment = HorizontalAlignment.Stretch;
    g.VerticalAlignment = VerticalAlignment.Stretch;

    // add grid line to show columns border
    g.ShowGridLines = true;

    ColumnDefinition columnDefinitionForPath = new ColumnDefinition();
    columnDefinitionForPath.Width = new GridLength(4, GridUnitType.Star);

    ColumnDefinition columnDefinitionForEmpty = new ColumnDefinition();
    columnDefinitionForEmpty.Width = new GridLength(6, GridUnitType.Star);

    g.ColumnDefinitions.Add(columnDefinitionForPath);
    g.ColumnDefinitions.Add(columnDefinitionForEmpty);

    var p1 = new Path();
    p1.Stroke = new SolidColorBrush(Colors.Brown);
    p1.StrokeThickness = 2;
    p1.Stretch = Stretch.Fill;
    p1.HorizontalAlignment = HorizontalAlignment.Stretch;

    var b1 = new Binding
    {
        // modified path data
        Source = "M 10,100 C 10,300 300,-200 300,100"
    };
    BindingOperations.SetBinding(p1, Path.DataProperty, b1);

    var p2 = new Path();
    p2.Stroke = new SolidColorBrush(Colors.Brown);
    p2.StrokeThickness = 2;
    p2.Stretch = Stretch.Fill;
    p2.HorizontalAlignment = HorizontalAlignment.Stretch;

    var b2 = new Binding
    {
        // modified path data
        Source = "M 100,10 C 100,30 -200,100 100,300"
    };
    BindingOperations.SetBinding(p2, Path.DataProperty, b2);

    g.Children.Add(p1);
    g.Children.Add(p2);
    Grid.SetColumn(p1, 0);
    Grid.SetColumn(p2, 1);

    return g;
}

更新:

通常情况下,Grid会自行伸展和扩展,根本不需要"fill it with empty space"。这里的问题是由于您将 Grid 放在 objectVisual 中,这显然是 ContentControl(您没有在 [=72= 中明确说明) ]).因此,您应该使 objectVisual 成为派生自 Panel 的类型,比如说,另一个 Grid

然后,替换为:

objectVisual.Content = ChangeContentObject();

有了这个:

objectVisual.Children.Add(ChangeContentObject());

你会得到你想要的:

更新二:

好吧,您的 pastebin 代码与您的原始代码略有不同,尽管那样也行得通,但您误解了我所说的 Grid。你不需要 var gridChild,你可以将第二列留空,就像你原来的问题一样。注意我把它注释掉了。因此,为了澄清,我在下面 posting 了完整的 MCVE 示例代码。结果看起来就像我之前 post 在上面编辑的第二张图片。

用户控件 CS:

public partial class SilverlightControl3 : UserControl
{
    public SilverlightControl3()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        objectVisual.Children.Add(ChangeContentObject());
    }

    private Grid ChangeContentObject()
    {
        Grid g = new Grid();
        g.Background = new SolidColorBrush(Colors.Red);

        g.HorizontalAlignment = HorizontalAlignment.Stretch;
        g.VerticalAlignment = VerticalAlignment.Stretch;

        // add grid line to show columns border
        g.ShowGridLines = true;

        ColumnDefinition columnDefinitionForPath = new ColumnDefinition();
        columnDefinitionForPath.Width = new GridLength(4, GridUnitType.Star);

        ColumnDefinition columnDefinitionForEmpty = new ColumnDefinition();
        columnDefinitionForEmpty.Width = new GridLength(6, GridUnitType.Star);

        g.ColumnDefinitions.Add(columnDefinitionForPath);
        g.ColumnDefinitions.Add(columnDefinitionForEmpty);

        var p1 = new Path();
        p1.Stroke = new SolidColorBrush(Colors.Blue);
        p1.StrokeThickness = 2;
        p1.Stretch = Stretch.Fill;
        p1.HorizontalAlignment = HorizontalAlignment.Stretch;

        g.Children.Add(p1);
        Grid.SetColumn(p1, 0);

        var b1 = new Binding
        {
            Source = "M 10,100 C 10,300 300,-200 300,100"
        };
        BindingOperations.SetBinding(p1, Path.DataProperty, b1);

        // you dont necessarily need this here, you can keep it empty like before
        /*
        var gridChild = new Grid();
        g.Children.Add(gridChild);
        Grid.SetColumn(gridChild, 1);
        */

        return g;
    }
}

用户控件XAML:

<UserControl x:Class="SilverlightApplication4.SilverlightControl3"
    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"
    d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid x:Name="objectVisual" />
    </Grid>
</UserControl>

主窗口:

<UserControl x:Class="SilverlightApplication4.MainPage"
    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:SilverlightApplication4"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot">
        <local:SilverlightControl3 />
    </Grid>
</UserControl>