Setvalue在网格中添加usercontrol不起作用(Setvalue to add usercontrol in a grid not work)

编程入门 行业动态 更新时间:2024-10-26 08:28:13
Setvalue在网格中添加usercontrol不起作用(Setvalue to add usercontrol in a grid not work)

我想以编程方式在一行中插入用户控件。 这是我的userControl:

public sealed partial class ItemWeek : UserControl { public string nome {get;set;} private string luogo { get; set; } public DateTime dataInizio { get; set; } public DateTime dataFine { get; set; } public ItemWeek() { this.InitializeComponent(); } public ItemWeek(string nome, string luogo, DateTime dataInizio, DateTime dataFine) { this.nome = nome; this.luogo = luogo; this.dataInizio = dataInizio; this.dataFine = dataFine; } } <Grid> <Grid Height="60" Width="80"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{x:Bind nome}" FontSize="23" VerticalAlignment="Center" HorizontalAlignment="Center"/> <TextBlock Grid.Row="1" Text="{x:Bind luogo}" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid> </Grid>

我想要做的只是将控件放在网格的一行中,方法setvalue不适用于用户控件。

grid1.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); ItemWeek ite = new ItemWeek("string", "string", dat, dat1); ite.SetValue(Grid.RowProperty, 0); ite.SetValue(Grid.ColumnProperty, 0); grid1.Children.Add(ite);

但如果我尝试插入文本块工程:

TextBlock txt = new TextBlock(); txt.Text = "teeeext"; txt.SetValue(Grid.RowProperty, 0); txt.SetValue(Grid.ColumnProperty, 0); grid1.Children.Add(txt);

为什么以及如何做? 非常感谢。

I want to programmatically insert a user control in a row. This is my userControl:

public sealed partial class ItemWeek : UserControl { public string nome {get;set;} private string luogo { get; set; } public DateTime dataInizio { get; set; } public DateTime dataFine { get; set; } public ItemWeek() { this.InitializeComponent(); } public ItemWeek(string nome, string luogo, DateTime dataInizio, DateTime dataFine) { this.nome = nome; this.luogo = luogo; this.dataInizio = dataInizio; this.dataFine = dataFine; } } <Grid> <Grid Height="60" Width="80"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{x:Bind nome}" FontSize="23" VerticalAlignment="Center" HorizontalAlignment="Center"/> <TextBlock Grid.Row="1" Text="{x:Bind luogo}" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid> </Grid>

I want to do is simply place the control in a row of the grid, the method setvalue does not work for the user control.

grid1.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); ItemWeek ite = new ItemWeek("string", "string", dat, dat1); ite.SetValue(Grid.RowProperty, 0); ite.SetValue(Grid.ColumnProperty, 0); grid1.Children.Add(ite);

but if I try to insert a textblock works:

TextBlock txt = new TextBlock(); txt.Text = "teeeext"; txt.SetValue(Grid.RowProperty, 0); txt.SetValue(Grid.ColumnProperty, 0); grid1.Children.Add(txt);

why and how can I do? thanks a lot.

最满意答案

你忘了在第二个构造函数中调用InitializeComponent() 。

要么像这样直接调用它:

public ItemWeek(string nome, string luogo, DateTime dataInizio, DateTime dataFine) { InitializeComponent(); this.nome = nome; this.luogo = luogo; this.dataInizio = dataInizio; this.dataFine = dataFine; }

或者像这样调用无参数构造函数:

public ItemWeek(string nome, string luogo, DateTime dataInizio, DateTime dataFine) : this() { this.nome = nome; this.luogo = luogo; this.dataInizio = dataInizio; this.dataFine = dataFine; }

可能根本不需要第二个构造函数,因为您也可以使用这样的对象初始化程序:

var ite = new ItemWeek { nome = "string", luogo = "string", dataInizio = dat, dataFine = dat1 };

您还应该使用静态方法Grid.SetColumn()和Grid.SetRow()而不是SetValue() :

Grid.SetRow(ite, 0); Grid.SetColumn(ite, 0);

只要您没有设置除默认值0之外的任何其他值,就不一定要调用这些方法。

You forgot to call InitializeComponent() in the second constructor.

Either call it directly like this:

public ItemWeek(string nome, string luogo, DateTime dataInizio, DateTime dataFine) { InitializeComponent(); this.nome = nome; this.luogo = luogo; this.dataInizio = dataInizio; this.dataFine = dataFine; }

Or call the parameterless constructor like this:

public ItemWeek(string nome, string luogo, DateTime dataInizio, DateTime dataFine) : this() { this.nome = nome; this.luogo = luogo; this.dataInizio = dataInizio; this.dataFine = dataFine; }

There is probably no need to have the second constructor at all, because you could also use an object initializer like this:

var ite = new ItemWeek { nome = "string", luogo = "string", dataInizio = dat, dataFine = dat1 };

You should also use the static methods Grid.SetColumn() and Grid.SetRow() instead of SetValue():

Grid.SetRow(ite, 0); Grid.SetColumn(ite, 0);

Calling these methods is not strictly necessary, as long as you do not set any other value than the default value 0.

更多推荐

本文发布于:2023-08-07 23:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   不起作用   usercontrol   Setvalue   grid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!