我需要一些关于Powershell和WPF的帮助。
我正在修改某人使用WPF和PowerShell创建的加载项。因此,我不能100%确定xaml结构是如何布局的。我想要做的是修改运行时生成的某个元素的Text属性。我使用了Snoop2.8.0并找到了我要修改的元素,它有一个Name属性" FreeTextBox"。我打算使用FindName方法来获取元素,但FindName方法不返回任何内容。
我在一些帖子中读到有一个名为contentPresenter的单独的东西,其中存储了这些运行时元素,我需要引用它来到我的文本框,但是,我不知道如何做到这一点。如果有人能指出我正确的方向,我将不胜感激。
此致 伊利亚斯
编辑: 表格代码
<?xml version="1.0" encoding="utf-8"?>
<WPF:DSWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WPF="clr-namespace:dataStandard.UI.View;assembly=dataStandard.UI" xmlns:system="clr-namespace:System;assembly=mscorlib" x:Name="InventorWindow" WindowStyle="ToolWindow" ShowInTaskbar="False" Topmost="True" SizeToContent="WidthAndHeight" Background="#F0F0F0" ResizeMode="NoResize">
<WPF:DSWindow.Resources>
<Style x:Key="DynamicDataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ComboBox ItemsSource="{Binding ListValues}" Text="{WPF:ValidatedBinding Value}" IsEditable="True"></ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}" />
</Trigger>
<DataTrigger Binding="{Binding ListValues.Count}" Value="0">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox Text="{WPF:ValidatedBinding Value}"></TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Typ}" Value="DateTime">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<DatePicker SelectedDate="{WPF:ValidatedBinding Value, StringFormat='dd/MM/yyyy'}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Typ}" Value="Boolean">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ComboBox SelectedItem="{WPF:ValidatedBinding Value}">
<system:Boolean>True</system:Boolean>
<system:Boolean>False</system:Boolean>
</ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnly}" Value="True">
<Setter Property="Background" Value="#F0F0F0"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DatePicker}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="FileNameStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding CreateMode}" Value="True"></Condition>
<Condition Binding="{Binding ElementName=NumSchms, Path=IsEnabled}" Value="True"></Condition>
</MultiDataTrigger.Conditions>
<Setter Property="Text" Value="{Binding UIString[LBL59]}"></Setter>
<Setter Property="IsReadOnly" Value="True"></Setter>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding CreateMode}" Value="True"></Condition>
<Condition Binding="{Binding ElementName=NumSchms, Path=IsEnabled}" Value="False"></Condition>
</MultiDataTrigger.Conditions>
<Setter Property="Text" Value="{Binding Prop[DocNumber].Value}"></Setter>
</MultiDataTrigger>
<DataTrigger Binding="{Binding EditMode}" Value="True">
<Setter Property="Background" Value="#F0F0F0" />
<Setter Property="IsReadOnly" Value="True"></Setter>
<Setter Property="Text" Value="{Binding Prop[_FileName].Value}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="NumSchmStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding CreateMode}" Value="True">
<Setter Property="ItemsSource" Value="{Binding PsList[GetNumSchms]}"></Setter>
<Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding EditMode}" Value="True">
<Setter Property="Text" Value="{Binding UIString[LBL27]}"></Setter>
<Setter Property="Background" Value="#F0F0F0" />
<Setter Property="IsEditable" Value="True"></Setter>
<Setter Property="IsReadOnly" Value="True"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Null}">
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="BorderBrush" Value="#ff0000"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<XmlDataProvider x:Key="ComboDataSource" Source="C:\CHM Vault\Libraries\data.xml"></XmlDataProvider>
<XmlDataProvider x:Key="JobCodeDataSource" Source="C:\CHM Vault\Libraries\jobcode.xml"></XmlDataProvider>
</WPF:DSWindow.Resources>
<Window.Style>
<Style TargetType="{x:Type WPF:DSWindow}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding CreateMode}" Value="True"></Condition>
<Condition Binding="{Binding CopyMode}" Value="True"></Condition>
</MultiDataTrigger.Conditions>
<Setter Property="Title">
<Setter.Value>
<MultiBinding StringFormat="{}{0} - '{1}'">
<Binding Path="UIString[LBL60]" />
<Binding Path="PathAndFileNameHandler.OriginalFileName" />
</MultiBinding>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding CreateMode}" Value="True"></Condition>
<Condition Binding="{Binding CopyMode}" Value="False"></Condition>
</MultiDataTrigger.Conditions>
<Setter Property="Title">
<Setter.Value>
<MultiBinding StringFormat="{}{0} - '{1}'">
<Binding Path="UIString[LBL24]" />
<Binding Path="PathAndFileNameHandler.OriginalFileName" />
</MultiBinding>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<DataTrigger Binding="{Binding CreateMode}" Value="False">
<Setter Property="Title">
<Setter.Value>
<MultiBinding StringFormat="{}{0} - '{1}'">
<Binding Path="UIString[LBL25]" />
<Binding Path="PathAndFileNameHandler.FileName" />
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
<DockPanel HorizontalAlignment="Left" Height="Auto" Margin="20" VerticalAlignment="Top" Width="Auto">
<Grid Margin="0,10,0,0" DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="White" BorderThickness="5" VerticalAlignment="Bottom" HorizontalAlignment="Center" Visibility="{Binding CopyMode, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="5,0,5,22">
<Image VerticalAlignment="Center" Width="120" Height="120" Source="{Binding Prop[Thumbnail].Value}" />
</Border>
<Button Command="{Binding CloseWindowCommand, ElementName=InventorWindow}" IsEnabled="True" Grid.Column="2" Margin="0,5,0,5" Width="80" HorizontalAlignment="Right" VerticalAlignment="Center" ToolTipService.ShowOnDisabled="True" ToolTip="{Binding UIString[LBL35], FallbackValue=Cancel}" Content="{Binding UIString[BTN1], FallbackValue=OK}" />
<Button Grid.Column="4" Width="80" HorizontalAlignment="Right" Margin="0,5,0,5" VerticalAlignment="Center" Command="{Binding CancelWindowCommand, ElementName=InventorWindow}" IsCancel="true" Content="{Binding UIString[BTN2], FallbackValue=Cancel}" />
</Grid>
<Grid Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" DockPanel.Dock="Left">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid Name="GeneralFileProp" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="350" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="{Binding UIString[LBL10], FallbackValue=Number Scheme}" />
<ComboBox Grid.Column="1" Grid.Row="0" x:Name="NumSchms" DisplayMemberPath="Name" SelectedIndex="0" Style="{StaticResource NumSchmStyle}" />
<Label Grid.Row="2" Grid.Column="0" Content="{Binding UIString[LBL31], FallbackValue=Number}" />
<WPF:DSNumSchemeCtrl Grid.Row="2" Grid.Column="1" Scheme="{Binding SelectedItem, ElementName=NumSchms}" IsEnabled="{Binding IsEnabled, ElementName=NumSchms}" GeneratedNumberProperty="DocNumber" />
<Label Content="Folder" Grid.Row="4" Grid.Column="0" Visibility="{Binding CreateMode, Converter={StaticResource BooleanToVisibilityConverter}}" />
<WrapPanel Name="BreadCrumb" Grid.Column="1" Grid.Row="4" ItemWidth="165">
<WrapPanel.Resources>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Margin" Value="0,5,15,0" />
<Setter Property="ToolTip" Value="{Binding Path=SelectedValue.Name, RelativeSource={RelativeSource Self}}" />
</Style>
</WrapPanel.Resources>
</WrapPanel>
<Label Content="Description" Grid.Column="0" Grid.Row="6" />
<TextBox Text="{Binding Prop[Description].Value}" Grid.Column="1" Grid.Row="6" x:Name="Description" VerticalScrollBarVisibility="Auto" Height="50" TextWrapping="Wrap" AcceptsReturn="True" />
<Label Content="Path" Grid.Column="0" Grid.Row="8" />
<TextBox Text="{Binding PathAndFileNameHandler.Path}" Grid.Column="1" Grid.Row="8" IsReadOnly="True" Background="#F0F0F0" />
<Label Content="File Name" Grid.Row="10" Grid.Column="0" />
<TextBox Grid.Row="10" Grid.Column="1" x:Name="FILENAME" Style="{StaticResource FileNameStyle}" />
</Grid>
<Grid Name="ModelProp" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="350" />
</Grid.ColumnDefinitions>
<Label Content="Category" Grid.Row="0" Grid.Column="0" />
<ComboBox x:Name="ItemCategory" Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Source={StaticResource ComboDataSource}, XPath=/Data/ItemCat/*}" DisplayMemberPath="@Name" SelectedValue="{Binding Prop[Item Category].Value}" SelectedValuePath="@Name" IsEnabled="{Binding CreateMode}" />
<!-- <ComboBox x:Name="Categories" DisplayMemberPath="Name" SelectedItem="{Binding DynamicPropertiesCategory}" ItemsSource="{Binding PsList[GetCategories]}" SelectedIndex="0" Grid.Row="0" Grid.Column="1" IsEnabled="{Binding CreateMode}" /> -->
<Label Content="Model" Grid.Row="2" Grid.Column="0" />
<ComboBox x:Name="Model" Grid.Column="1" Grid.Row="2" ItemsSource="{Binding Source={StaticResource ComboDataSource}, XPath=/Data/ModelStd/*}" DisplayMemberPath="@Name" Text="{Binding Prop[Model].Value}" SelectedValue="{Binding Prop[Model].Value}" SelectedValuePath="@Name" IsEditable="True" />
<Label Content="Order Code" Grid.Row="4" Grid.Column="0" />
<TextBox Text="{Binding Prop[Order Code].Value}" Grid.Row="4" Grid.Column="1" />
<Label Content="Rating" Grid.Row="6" Grid.Column="0" />
<TextBox Text="{Binding Prop[Rating].Value}" Grid.Row="6" Grid.Column="1" />
<Label Content="Brand" Grid.Row="8" Grid.Column="0" />
<TextBox Text="{Binding Prop[Brand].Value}" Grid.Row="8" Grid.Column="1" />
<Label Content="Manufacturing Length" Grid.Row="10" Grid.Column="0" />
<TextBox Text="{Binding Prop[Manufacturing Length].Value}" Grid.Row="10" Grid.Column="1" />
</Grid>
<Grid Name="DrawingProp" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
<RowDefinition Height="8" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="350" />
</Grid.ColumnDefinitions>
<Label Content="Job Code" Grid.Row="0" Grid.Column="0" />
<ComboBox x:Name="JobCode" Grid.Column="1" Grid.Row="0" ItemsSource="{Binding Source={StaticResource JobCodeDataSource}, XPath=/JobCode/*}" DisplayMemberPath="@Code" SelectedValue="{Binding Prop[Job Code].Value}" SelectedValuePath="@Code" IsEnabled="True"/>
<Label Content="Client" Grid.Row="2" Grid.Column="0" />
<TextBox Name="Client" Text="{Binding Prop[Client].Value}" Grid.Row="2" Grid.Column="1" IsEnabled="False" />
<Label Content="Project Title" Grid.Row="4" Grid.Column="0" />
<TextBox Name="ProjectTitle" Text="{Binding Prop[Project Title].Value}" Grid.Row="4" Grid.Column="1" IsEnabled="False"/>
<TextBox Name="Treatment" Text="{Binding Prop[Treatment].Value}" Grid.Row="6" Grid.Column="1" IsEnabled="True"/>
</Grid>
</Grid>
</DockPanel>
</WPF:DSWindow>
行为准则
function InitializeWindow
{
$dsWindow.Width = 500
$dsWindow.Height = 400
$Prop["Job Code"].add_PropertyChanged({p
param( $parameter)
StateChanged
})
If (($Prop["_FileExt"].Value -eq "IDW") -or ($Prop["_FileExt"].Value -eq "DWG"))
{
$dsWindow.FindName("ModelProp").Visibility="Collapsed"
$dsWindow.FindName("DrawingProp").Visibility="Visible"
}
Else
{
$dsWindow.FindName("ModelProp").Visibility="Visible"
$dsWindow.FindName("DrawingProp").Visibility="Collapsed"
}
$mWindowName = $dsWindow.Name
switch($mWindowName)
{
"InventorWindow"
{
#rules applying for Inventor
}
"AutoCADWindow"
{
#rules applying for AutoCAD
}
default
{
#rules applying commonly
}
}
if ($Prop["_CreateMode"].Value)
{
$mappedRootPath = $Prop["_VaultVirtualPath"].Value + $Prop["_WorkspacePath"].Value
$mappedRootPath = $mappedRootPath -replace "\\", "/" -replace "//", "/"
if ($mappedRootPath -eq '')
{
$mappedRootPath = '$'
}
$rootFolder = $vault.DocumentService.GetFolderByPath($mappedRootPath)
$root = New-Object PSObject -Property @{ Name = $rootFolder.Name; ID=$rootFolder.Id }
AddCombo -data $root
}
}
function AddinLoaded
{
#Executed when DataStandard is loaded in Inventor
}
function AddinUnloaded
{
#Executed when DataStandard is unloaded in Inventor
}
function GetNumSchms
{
try
{
$numSchems = $vault.DocumentService.GetNumberingSchemesByType('Activated')
if ($numSchems.Count -gt 0)
{
$list = New-Object 'System.Collections.Generic.List[System.Object]'
foreach ($item in $numSchems)
{
if ($item.IsDflt)
{
$list.Insert(0,$item)
}
else
{
$list.Add($item)
}
}
return $list
}
}
catch [System.Exception]
{
#[System.Windows.MessageBox]::Show($error)
}
}
function GetCategories
{
if ($Prop["_CreateMode"].Value)
{
$cats = $vault.CategoryService.GetCategoriesByEntityClassId("FILE", $true)
if ($cats.Count -gt 0)
{
$list = New-Object 'System.Collections.Generic.List[System.Object]'
foreach ($item in $cats)
{
$list.Add($item)
}
return $list
}
}
if ($Prop["_EditMode"].Value)
{
return GetCategoryByFileName $Prop["_FileName"].Value
}
}
function OnPostCloseDialog
{
$mWindowName = $dsWindow.Name
switch($mWindowName)
{
"InventorWindow"
{
#rules applying for Inventor
If (($Prop["_FileExt"].Value -eq "IDW") -or ($Prop["_FileExt"].Value -eq "DWG"))
{
If ($Prop["DocNumber"].Value.substring(0,1) -eq "Q")
{
$Prop["DocNumber"].Value=$Prop["DocNumber"].Value.substring($Prop["DocNumber"].Value.length-5,5)
}
}
}
"AutoCADWindow"
{
#rules applying for AutoCAD
}
default
{
#rules applying commonly
}
}
}
function GetCategoryByFileName($fileName)
{
$filePropDefs = $vault.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE");
$fileNamePropDef = $filePropDefs | Where-Object {$_.SysName -eq "ClientFileName"}
$searchCond = New-Object -type Autodesk.Connectivity.WebServices.SrchCond
$searchCond.PropDefId = $fileNamePropDef.Id
$searchCond.PropTyp = [Autodesk.Connectivity.WebServices.PropertySearchType]::SingleProperty
$searchCond.SrchTxt = $fileName
$searchCond.SrchOper = 3
$searchCond.SrchRule = [Autodesk.Connectivity.WebServices.SearchRuleType]::Must
$bookmark = ""
$status = $null
$cat = $null
$findFilesBySearchConditions = $vault.DocumentService.FindFilesBySearchConditions($searchCond, $null, $null, $false, $true, [ref]$bookmark, [ref]$status)
if ($findFilesBySearchConditions -ne $null)
{
$cat = New-Object Autodesk.Connectivity.WebServices.Cat
$cat.Id = $findFilesBySearchConditions[0].Cat.CatId
$cat.Name = $findFilesBySearchConditions[0].Cat.CatName
}
return $cat;
}
function StateChanged
{
$jobdata="C:\ProgramData\Autodesk\Vault 2016\Extensions\DataStandard\CAD\Configuration\jobcode.xml"
$jobcodes = [xml](Get-Content $jobdata)
$job=$jobcodes.JobCode.Job|where {$_.Code -eq $Prop["Job Code"].Value}
$Prop["Project Title"].Value=$job.Title
$Prop["Client"].Value=$job.Client
#[System.Windows.Forms.MessageBox]::Show($dswindow.FindName("FreeTextBox").Text)
#$dsWindow.FindName("FreeTextBox").Text=$Prop["Job Code"].Value
}
从第二个代码的最后一行可以看出,我试图使用findname来获取从主应用程序创建的FreeTextBox。不幸的是,我没有创建文本框的代码,因为这是一个从主应用程序调用的插件。我使用snoop来找出元素本身的属性,但是堆栈还不允许我发布图像。
答案 0 :(得分:0)
FreeTextBox可能在另一个用户控件或datatemplate中加载到您的窗口,因此位于不同的NameScope中。 FindName仅在当前的名称范围内查找元素。
您需要使用VisualTreeHelper API以递归方式遍历窗口的整个可视子树,并检查每个元素的name属性以便找到FreeTextBox。只需使用GetChildrenCount
和GetChild
方法。