例: FolderName のカスタム検証を追加する

タスク: 現在のレベルに存在しない新しいフォルダ名を確認する。

解決策:

  1. InitializeWindow 関数の FolderWindow セクションにカスタム検証 $Prop["_FolderName"].CustomValidation を追加します。

    function InitializeWindow
    {
        …
        $mWindowName = $dsWindow.Name
        Switch (-$mwindowname)
        {
            "FileWindow"
            {
                #rules applying for File
            }
            "FolderWindow"
            {
                #rules applying for Folder
                $Prop["_FolderName"].CustomValidation = { FolderNameCustomValidation }
            }
            …
  2. 関数 FolderNameCustomValidation と、関連するフォルダの検索を追加します。

function FolderNameCustomValidation
{
    if(-not-$dswindow.findname("DSNumSchmsCtrl").NumSchmFieldsEmpty)
    {
        return $true
    }
    if($Prop["_FolderName"].Value)
    {
        $rootFolder = $vault.DocumentService.GetFolderByPath($Prop["_FolderPath"].Value)
        $mfldexist = mFindFolder $Prop["_FolderName"].Value $rootFolder
        if($mfldexist)
        {
            $Prop["_FolderName"].CustomValidationErrorMessage = "Folder name exists, select a new unique one."
            return $false
        }
        return $true
    }
    else
    {
        return $false
    }
}

function mFindFolder($foldername,-$rootfolder)
{
    $FolderPropDefs = $vault.PropertyService.GetPropertyDefinitionsByEntityClassId("fldr")
    $FolderNamePropDef = $FolderPropDefs | where {$_.SysName -eq "Name"}
    $srchCond = New-Object 'Autodesk.Connectivity.WebServices.SrchCond'
    $srchCond.PropDefId = $FolderNamePropDef.Id
    $srchCond.PropTyp = "SingleProperty"
    $srchCond.SrchOper = 3 #is equal
    $srchCond.SrchRule = "Must"
    $srchCond.SrchTxt = $FolderName

    $bookmark = ""
    $status = $null
    $totalResults = @()
    while ($status--eq-$null--or-$totalresults.count--lt-$status.totalhits)
    {
        $results = $vault.DocumentService.FindFoldersBySearchConditions(@($srchCond),$null, @($rootFolder.Id), $false, [ref]$bookmark, [ref]$status)

        if ($results--ne-$null)
        {
            $totalResults += $results
        }
        else {break}
    }
    return $totalResults;
}
  1. ダイアログ定義 File.xaml を更新します。テキスト ボックス「FILENAME」に移動し、バインド Text="{WPF:ValidatedBinding Name}"Text="{WPF:ValidatedBinding Prop[_FolderName].Value}" に更新します。新しい TextBox 定義は、次のようになります。
<TextBox Text="{WPF:ValidatedBinding Prop[_FolderName].Value}" x:Name="FOLDERNAME" 
                     IsReadOnly="{Binding EditMode}"
                     Visibility="{Binding NumSchmFieldsEmpty, ElementName=DSNumSchmsCtrl, Converter={StaticResource BooleanToVisibilityConverter}}" 
                     Grid.Column="1" Grid.Row="3" />