在WPF中以编程方式用TextBoxes填充网格 [英] Filling Grid with TextBoxes programmatically in WPF
问题描述
中定义了两个 Grid
和几个 TextBox
XAML
。我想在运行时将这些 TextBox
es放入 Grid
s中(我知道哪个 TextBox 应该在启动应用程序后放入 Grid
)。
我认为它可以简单地将行定义添加到我的C#代码中的 Grid
,但文本框绑定到一些 Style
。
也许有一个简单的解决方案(完全在XAML中 - 首选)?
所以问题是 - 如何在运行期间用项目(文本框)和XAML中定义的网格动态地填充网格?这两个项目绑定到一些样式等。
预先感谢回复和提示!
干杯
我没有完全明白你的问题,但是如果我理解正确,你需要使用XAML的网格内的TextBox。 如果是这种情况
您可以这样做:
< GridViewColumn CellTemplate ={StaticResource editColumnTemplate}/>
然后在一个静态资源中:
< DataTemplate x:Key =editColumnTemplate>
< TextBox Text ={Binding Path = DataContext.Foo}/>
< / DataTemplate>
HTH
Let's assume that I have defined two Grid
s and few TextBox
es in my XAML
. I want to put these TextBox
es in the Grid
s at runtime (I know which TextBox
should be put in which Grid
after starting the application).
I thought it would be possible to simply add a row definition to the Grid
in my C# code, however the text boxes bind to some Style
.
Maybe there is a simple solution (entirely in XAML - preferred)?
So the question is - how to dynamically fill the grid during the runtime with the items (text boxes) and the grid defined in XAML? Both items bind to some styles etc.
Thanks in advance for the replies and hints!
Cheers
解决方案 I don't fully get you question, but if I understood correctly you want TextBoxes inside your grid using XAML.
If this is the case
you can do:
<GridViewColumn CellTemplate="{StaticResource editColumnTemplate}"/>
and then in a static resource:
<DataTemplate x:Key="editColumnTemplate">
<TextBox Text="{Binding Path=DataContext.Foo}" />
</DataTemplate>
HTH
这篇关于在WPF中以编程方式用TextBoxes填充网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!