我有一个简单的WPF MVVM应用程序,它包含一个带按钮的MainWindow。我需要启用/禁用此按钮,具体取决于TextBox的验证,该TextBox是主窗口中显示的UserControl(View)的一部分。
我可以在主窗口中显示不同的视图(一次一个)。但我没有任何运气绑定验证结果(例如文本框或复选框)以与主窗口的一部分按钮(启用/禁用)同步。
以下是我的代码示例: MainWindow.xaml
<Window x:Class="ChildToParent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="MainGrid" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="256"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" x:Name="currentView" VerticalAlignment="Top"/>
<Button Content="Submit" Grid.Row="1" Margin="0,10,54,0" Padding="10,3,10,3" HorizontalAlignment="Right"
Name="buttonSubmit" VerticalAlignment="Top" Height="26" Width="62">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=(Validation.HasError)}" Value="false" />
<!--<Condition Binding="{Binding ElementName=TextBoxFirstName, Path=(Validation.HasError)}" Value="false" />-->
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="True" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
这是ChildView.xaml
<UserControl x:Class="ChildToParent.Views.ChildView"
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:local="clr-namespace:ChildToParent.Validation"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="520">
<Grid Height="250" Width="520" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="109" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- Labels -->
<TextBlock Text="FirstName" Margin="0,10,10,222" HorizontalAlignment="Right" Width="59" />
<!-- TextBlocks -->
<TextBox Name="TextBoxFirstName" Grid.Column="1" Margin="0,5,0,225" Width="391" HorizontalAlignment="Left">
<TextBox.Text>
<Binding Path="FirstName" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<local:TextBoxNotEmptyValidationRule x:Name="FirstNameValidation" ValidatesOnTargetUpdated="True"
Message="You must enter a first name."/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
如何绑定作为UserControl(文本框/复选框)一部分的控件的操作/结果,以触发作为主窗口一部分的控件(按钮)? 我用google搜索了这个,但是没有找到一个例子,我可以做一些不可能的事情吗?我需要你的帮助......