範例 - 為 FolderNames 加入自訂驗證

工作:檢查目前層級沒有的新資料夾名稱。

解決方案:

  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. 更新 Dialog definition File.xaml:導覽至 Textbox "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" />