Как я могу получить доступ из кода в WPF? - PullRequest
0 голосов
/ 16 февраля 2020

Я создал изображение в 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>

Ответы [ 2 ]

0 голосов
/ 16 февраля 2020
 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();
0 голосов
/ 16 февраля 2020

Вы можете использовать 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");
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...