【问题标题】:How can I access <Image Name="AddImg"/> from Code in WPF?如何从 WPF 中的代码访问 <Image Name="AddImg"/>?
【发布时间】:2020-02-16 12:35:28
【问题描述】:

我在 XAML 中创建了一个图像,我想从代码中访问它以动态修改源。到目前为止我无法访问。有什么解决办法吗?

<Window x:Class="DDSV2.MasterMain"
    Name="MainWw"
    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:DDSV2"
    mc:Ignorable="d"
    Title="MasterMain" Height="1123.439" Width="1822.363" Loaded="Window_Loaded">
<Grid Name="MainGrid">
    <ListView x:Name="MainMasterLv" Height="333" Margin="10,10,1494,0" VerticalAlignment="Top" SelectionChanged="MainMasterLv_SelectionChanged">
        <ListView.View>
            <GridView x:Name="GridVw">
                <GridViewColumn x:Name="DataGridCn"  Header="">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate x:Name="DataTmp">
                            <StackPanel Orientation="Horizontal">
                                <Image x:Name="AddImg" Source="pack://application:,,,/Resources        /gears.png" MaxWidth="80" MaxHeight="80" Margin="3,3,3,3"/>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>

【问题讨论】:

  • @Clint 你不能这样做,因为图像在 DataTemplate 中。
  • 为什么需要从后面的代码中访问 Image 元素?您可以将其任何属性绑定到 ListView 使用的项类的属性。特别是&lt;Image Source="{Binding YourProperty}"/&gt;。当然,您也可以在一个或多个设置 Source 属性的属性上使用带有转换器的绑定,甚至是 DataTriggers
  • @Clemens 感谢您的想法。最后,我使用了 Binding 并更改了 Property 值。谢谢!

标签: c# wpf image xaml


【解决方案1】:
 public T FindChild<T>(DependencyObject parent, string childName)
   where T : DependencyObject
        {
            // Confirm parent and childName are valid. 
            if (parent == null) return null;

            T foundChild = null;

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                // If the child is not of the request child type child
                T childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild<T>(child, childName);

                    // If the child is found, break so we do not overwrite the found child. 
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }

并像这样使用它:

Image item =FindChild<Image>(DataGridCn.CellTemplate.LoadContent(), "AddImg");
string source = item.Source.ToString();

【讨论】:

    【解决方案2】:

    您可以使用 xml 方法,例如 xml linq :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
    
                XElement image = doc.Descendants().Where(x => x.Name.LocalName == "Image").FirstOrDefault();
                XNamespace nsN = image.GetNamespaceOfPrefix("x");
                XAttribute name = image.Attribute(nsN + "Name");
                name.SetValue("newName");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      相关资源
      最近更新 更多