我试图绑定文本框的TargetNullValue(如果重要的话,在DataTemplate内部)当然会出现以下错误...
“不能在类型的'TargetNullValue'属性上设置'Binding' “绑定””
由于与本Microsoft Example中的UWP不同,WPF绑定属性不是DependencyProperties。
这是违规代码的过度简化:
<TextBox Text="{Binding Path=TargetFile, TargetNullValue={Binding Path=NullValue}}"/>
TargetFile
绑定的声明与ValidationMask
完全相同,因此问题很简单,TargetNullValue
不是DependencyProperty。
There's an answer to another question with a MultiBinding
workaround for getting multiple parameters in,但是我看不到它如何更一般地映射到TargetNullValue
(如果您看下面的完整代码,还可以加上FallbackValue
和ValidationMask
。)< / p>
这是上下文中的完整代码(请注意,所有无效的数据绑定都被Wally[1-5]
替换,因此此版本运行并在应具有绑定的位置显示“ Wally”):
<Style x:Key="FileSelectionBlockSet" TargetType="{x:Type ItemsControl}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="{Binding Prompt}" FontSize="11" Margin="0,0,0,-9" Background="{x:Null}"/>
<TextBox x:Name="FilePath" HorizontalAlignment="Stretch" TabIndex="4" Margin="6" Grid.Row="1">
<TextBox.Text>
<Binding Path="TargetFile" TargetNullValue="Wally1" FallbackValue="Wally2">
<Binding.Converter>
<ts:FileInfoToStringConverter/>
</Binding.Converter>
<Binding.ValidationRules>
<ts:StringIsValidFileRule ValidationMask="Wally3" MustExist="true" ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<TextBox.ToolTip>
<Binding Path="ToolTip" TargetNullValue="Wally4" FallbackValue="Wally5" Mode="OneTime">
<Binding.Converter>
<ts:FileInfoNameToStringConverter/>
</Binding.Converter>
</Binding>
</TextBox.ToolTip>
</TextBox>
<Button x:Name="BrowseButton" Content="Browse" Width="75" TabIndex="5" Margin="0,6,6,6" Grid.Row="1" Grid.Column="1" Click="BrowseButton_Click"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsTabStop" Value="False"/>
</Style>
以及要分配的项目的C#:
public class FileSelectionBlock
{
public FileSelectionBlock(string prompt, string validationMask, string toolTip, DirectoryInfo startingDirectory = null, FileInfo targetFile = null)
{
Prompt = prompt;
ValidationMask = validationMask;
ToolTip = toolTip;
StartingDirectory = startingDirectory;
TargetFile = targetFile;
}
public string Prompt { get; set; }
public string ValidationMask { get; set; }
public string ToolTip { get; set; }
public FileInfo TargetFile { get; set; }
public DirectoryInfo StartingDirectory { get; set; }
}