我很确定这个问题不时被问到,因为我在这里找到了另一个内容大致相同的问题。但是当我尝试使用所有这些页面时,我只是卡住了......这就是我想要做的。我创建了一个用户控件来选择一个文件并在文本框中显示文件的路径。就像在 HTML 中一样(输入类型=文件)。这一切都很好,正如预期的那样。但是,当我尝试使用触发器 (FilePathIsValid) 更改文本框的颜色时,它就不起作用了。如上所述,依赖属性都可以正常工作。但是样式只是没有分配给文本框。这是我的 XAML - 谁能告诉我我做错了什么?(此处的代码,如果需要)
<UserControl x:Class="Project.Controls.SelectFileBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="clr-namespace:Project.Controls"
x:Name="ThisUserControl"
mc:Ignorable="d" d:DesignHeight="30" d:DesignWidth="300">
<UserControl.Resources>
<!-- This does not work... -->
<Style TargetType="{x:Type Controls:SelectFileBox}">
<Style.Triggers>
<Trigger Property="FilePathIsValid" Value="false">
<Setter Property="TextBoxBorderColor" Value="red"/>
</Trigger>
</Style.Triggers>
</Style>
<!-- Neither does this: FilePathIsValid can't be found -->
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="FilePathIsValid" Value="false">
<Setter Property="BorderBrush" Value="red"/>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Margin="3" IsEnabled="{Binding ElementName=ThisUserControl,Path=TextBoxIsEnabled}" Text="{Binding ElementName=ThisUserControl,Path=FilePath}" BorderThickness="{Binding ElementName=ThisUserControl, Path=TextBoxBorderThickness}"/>
<Button Grid.Column="1" Margin="0,3,3,3" Content="{Binding ElementName=ThisUserControl, Path=ButtonText}" Click="SelectFileClick"/>
</Grid>