gpt4 book ai didi

c# - Silverlight 网格未填满整个空间

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:55 25 4
gpt4 key购买 nike

我在 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

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

最佳答案

您的 Grid 有 2 列。第二列是空的,因此折叠起来,所以您看不到它。将某些东西绑定(bind)到它可以解决“问题”。另外,为这个演示修改了你的 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 会自行拉伸(stretch)和扩展,根本不需要“用空白填充它”。这里的问题是由于您将 Grid 放在 objectVisual 中,这显然是一个 ContentControl(您没有在你的帖子中说清楚)。因此,您应该使 objectVisual 成为派生自 Panel 的类型,例如,另一个 Grid

然后,替换为:

objectVisual.Content = ChangeContentObject();

用这个:

objectVisual.Children.Add(ChangeContentObject());

你会得到你想要的:

enter image description here

更新 2:

好吧,您的 pastebin 代码与您的原始代码略有不同,虽然这也可以工作,但您误解了我所说的关于 Grid 的内容。您不需要 var gridChild,您可以将第二列留空,就像您原来的问题一样。注意我把它注释掉了。所以,为了澄清,我发布了完整的 MCVE下面的示例代码。结果看起来就像我之前发布的第二张图片。

用户控件 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>

关于c# - Silverlight 网格未填满整个空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49153435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com