标签: uielementcollection

给定UIElementCollection,找到具有StyleA的所有元素,并在WPF中将它们更改为StyleB

我有一个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吗?

linq wpf styles uielementcollection

6
推荐指数
1
解决办法
6363
查看次数

如何从属性=某个值的UIElementCollection中选择一个子节点?

我有UniformGrid一些ButtonChildren.每个Button都有一个TagID,例如(dumbed down代码):

MyUniformGrid.Children.Add(new Button {
    Margin  = new Thickness(5),
    Tag     = Query.GetUInt32("id"),
    Width   = 200
});
Run Code Online (Sandbox Code Playgroud)

如何选择ButtonID为87 的子对象?(例如)

当我输入MyUniformGrid.Children.(添加后using System.Linq;)时,Intellisense没有弹出Linq方法.

c# linq children uielementcollection

5
推荐指数
1
解决办法
2136
查看次数

XamlWriter.Save() 未序列化 DependencyProperties

考虑来自我的 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)

c# wpf xaml uielementcollection xamlwriter

5
推荐指数
1
解决办法
1762
查看次数

标签 统计

uielementcollection ×3

c# ×2

linq ×2

wpf ×2

children ×1

styles ×1

xaml ×1

xamlwriter ×1