Silverlight网格没有填满整个空间

时间:2018-03-07 13:44:58

标签: c# asp.net .net xaml silverlight

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

函数返回网格:

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

我得到下一个结果:

enter image description here

为什么网格不能填满整个长度?

1 个答案:

答案 0 :(得分:0)

您的Grid有2列。第二列为空,因此折叠,因此您无法看到它。将东西绑定到它可以解决“问题”。此外,修改了此演示版的Path数据,因为您拥有的数据并不好。看看:

enter image description here

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会自行伸展和扩展,根本不需要“用空格填充”。这里的问题是由于您将Grid放在objectVisual内,这显然是ContentControl(您在帖子中没有明确说明)。因此,您应该将objectVisual设为从Panel派生的类型,比如说,另一个Grid

然后,替换它:

objectVisual.Content = ChangeContentObject();

用这个:

objectVisual.Children.Add(ChangeContentObject());

你会得到你想要的东西:

enter image description here

更新2:

嗯,您的 pastebin 代码与原始代码略有不同,虽然这也有用,但您误解了我对Grid所说的内容。您不需要var gridChild,您可以将第二列保留为空,就像在原始问题中一样。注意我评论了它。因此,为了澄清,我在下面发布了完整的MCVE示例代码。结果看起来就像我之前发布的第二张图片一样。

UserControl 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;
    }
}

UserControl 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>