3

要求:
当用户输入不存在的forlder名称时需要显示错误信息,如下图所示: 替代文字

问题:当用户单击“CreateNew”按钮时,我能够显示 UI,但无法在视图模型中调用方法

查看型号代码:

    public string this[string columnName]
    {
        get { return "The entered folder name doesn't exist."; }
    }

    RelayCommand createNewFolder;
    public RelayCommand CreateNewFolder
    {
        get
        {
            if (createNewFolder == null)
                createNewFolder = new RelayCommand(param => this.OnCreateNewFolder());
            return createNewFolder;

        }

    }
    public void OnCreateNewFolder()
    {
        MessageBox.Show("CreateNewFolder");
    }



RelayCommand.cs 可以在以下位置下载:http ://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mag200902MVVM&DownloadId=4357



Xaml 代码:

<Window.Resources>        
    <ControlTemplate x:Key="validationTemplate">
        <DockPanel LastChildFill="True">
            <Border Margin="5,5,0,0" DockPanel.Dock="Bottom" Background="Red">
            <StackPanel>
                <TextBlock Name="ErrorText"  Foreground="White" Background="Red" 
                                   FontSize="12" Padding="2" FontFamily="Trebuchet MS"                                                                                
                                   TextWrapping="Wrap"                                        
                                   Text="{Binding [0].ErrorContent}" ></TextBlock>
                <StackPanel Margin="0" Orientation="Horizontal">
                    <Button Content="Create New" Command="{Binding Path=CreateNewFolder}" Margin="10" Padding="5"></Button>
                    <Button Content="Cancel" Margin="10" Padding="5" ></Button>
                </StackPanel>
            </StackPanel>
            </Border>
            <AdornedElementPlaceholder Name="ErrorTextBox" />
        </DockPanel>
    </ControlTemplate>
    <Style x:Key="ValidationStyle" TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="BorderBrush" Value="Red" />
                <Setter Property="BitmapEffect">
                    <Setter.Value>
                        <BitmapEffectGroup>
                            <OuterGlowBitmapEffect GlowColor="Red" GlowSize="3" Noise="0.6"></OuterGlowBitmapEffect>
                        </BitmapEffectGroup>
                    </Setter.Value>
                </Setter> 
                <Setter Property="DataContext"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=DataContext}">
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


<Grid>
    <AdornerDecorator >
        <ComboBox IsEditable="True" FontSize="11" Margin="10" Width="250"      
              VerticalAlignment="Center" 
              Text="{Binding Path=StrText, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
              Validation.ErrorTemplate="{StaticResource validationTemplate}"
              Style="{StaticResource ValidationStyle}"></ComboBox>
    </AdornerDecorator>
</Grid>


请注意,我在样式中设置了 DataContext 属性:

 <Setter Property="DataContext" Value="{Binding RelativeSource={x:Static 
          RelativeSource.Self},Path=DataContext}">
 </Setter>    



请让我知道如何将方法绑定到验证模板中的按钮。

4

2 回答 2

2

您可以在 Binding 中引用 AdornerDecorators Child 的 DataContext。我认为这样的事情会奏效

<Button Content="Create New"
        Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type AdornerDecorator}},
                          Path=Child.DataContext.CreateNewFolder}"
        Margin="10" Padding="5"></Button>
于 2010-12-29T15:48:15.477 回答
0

这条线看起来很可疑:

createNewFolder = new RelayCommand(param => this.OnCreateNewFolder());

也许您应该将其替换为:

createNewFolder = new RelayCommand(OnCreateNewFolder);
于 2010-12-29T15:45:44.423 回答