我有一个MyGrid.Children UIElementCollection,我想在其中找到所有样式设置为StyleA的矩形,并将它们设置为StyleB.
如果可能的话我想使用LINQ,所以我可以避免讨厌的嵌套循环.
像这样的伪代码:
var Recs = from r in MyGrid.Children
where r.Style == StyleA && r.GetType() == typeof(Rectangle)
select r as Rectangle;
Run Code Online (Sandbox Code Playgroud)
然后:
foreach(Rectangle r in Recs)
r.Style = StyleB;
Run Code Online (Sandbox Code Playgroud)
LINQ大师可以帮助我改进我的LINQ-fu吗?
我有UniformGrid
一些Button
像Children
.每个Button
都有一个Tag
ID,例如(dumbed down代码):
MyUniformGrid.Children.Add(new Button {
Margin = new Thickness(5),
Tag = Query.GetUInt32("id"),
Width = 200
});
Run Code Online (Sandbox Code Playgroud)
如何选择Button
ID为87 的子对象?(例如)
当我输入MyUniformGrid.Children.
(添加后using System.Linq;
)时,Intellisense没有弹出Linq方法.
考虑来自我的 UserControl 的以下 XAML:
<TextBlock Text="HelloWorld" Loaded="TextBlock_OnLoaded" />
Run Code Online (Sandbox Code Playgroud)
以及相关的事件处理程序:
private void TextBlock_OnLoaded(object sender, RoutedEventArgs e)
{
var xaml = XamlWriter.Save(sender);
Console.WriteLine(xaml);
}
Run Code Online (Sandbox Code Playgroud)
加载 TextBlock 后,以下输出将写入控制台:
<TextBlock Text="HelloWorld" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
Run Code Online (Sandbox Code Playgroud)
现在考虑这个替代 XAML:
<ListBox ItemsSource="{Binding SomeCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="HelloWorld" Loaded="TextBlock_OnLoaded" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
现在,当加载 TextBlock 时,以下输出将写入控制台:
<TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
......
Run Code Online (Sandbox Code Playgroud)
请注意 TextProperty 不再被序列化。
如果在调用 XamlWriter.Save() 之前添加了以下 TextProperty 分配:
private void TextBlock_OnLoaded(object sender, RoutedEventArgs e)
{
var textBlock = sender as TextBlock;
if (textBlock != …
Run Code Online (Sandbox Code Playgroud)