2013-08-19 37 views
5

Tôi đã tìm kiếm và không thấy giải pháp.Chú giải công cụ không hiển thị khi không có lỗi xác thực WPF

Tôi chỉ có thể nhận được xác thực để hiển thị chú giải công cụ nếu tôi không đặt chú giải công cụ trong thuộc tính chú giải công cụ hộp kết hợp. Tôi muốn xem chú giải công cụ lỗi xác thực khi có một chú giải công cụ khác hiển thị chú giải công cụ từ thuộc tính combobox. Chú giải công cụ xác thực hiển thị tốt khi tôi xóa văn bản khỏi thuộc tính tooltip (tức là từ bảng thuộc tính cho hộp tổ hợp).

Các XAML trong Application.Resources (App.xaml) cho tooltip để hiển thị các lỗi xác nhận là

<Style x:Key="StandardComboBoxStyle" TargetType="{x:Type ComboBox}"> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="True"> 
      <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

Tôi cũng sử dụng một mẫu xác nhận cho Combobox như sau. Điều này nằm trong phần UserControl.Resources trong tệp cs kiểm soát người dùng.

<ControlTemplate x:Key="comboBoxValidationTemplate"> 
    <DockPanel Name="myDockPanel"> 
     <Border BorderBrush="Red" BorderThickness="3"> 
      <AdornedElementPlaceholder Name="MyAdorner" /> 
     </Border> 
     <TextBlock Text="*" FontWeight="Bold" FontSize="18" Foreground="Red" DockPanel.Dock="Left" /> 
    </DockPanel> 
</ControlTemplate> 

Bản thân kiểm soát được định nghĩa như sau. Lưu ý rằng có các tham chiếu khác không được xác định ở đây (nhưng hy vọng không thích hợp - hãy cho tôi biết nếu câu hỏi).

 <ComboBox x:Name="ExposureTime" SelectedValuePath="Content" 
     Text="{Binding ExposureTime, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" IsEditable="True" Validation.ErrorTemplate="{StaticResource comboBoxValidationTemplate}" 
     HorizontalContentAlignment="Right" FontSize="18" Margin="136,47,462,0" Height="27" VerticalAlignment="Top" GotFocus="ComboBox_GotFocus_1" LostFocus="ComboBox_LostFocus_1" PreviewTextInput="ExposureTime_PreviewTextInput" Opacity="{Binding BackgroundOpacity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontWeight="Thin" Style="{DynamicResource StandardComboBoxStyle}" SelectedValue="{Binding Mode=OneWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" IsTextSearchEnabled="False" ToolTip="My tooltip test."> 
     <ComboBoxItem Content="0.05"/> 
     <ComboBoxItem Content="0.1"/> 
     <ComboBoxItem Content="0.2" /> 
     <ComboBoxItem Content="1" /> 
     <ComboBoxItem Content="2" /> 
     <ComboBoxItem Content="5" /> 
     <ComboBoxItem Content="10" /> 
     <ComboBoxItem Content="20" /> 
     <ComboBoxItem Content="60" /> 
     <ComboBox.IsEnabled > 
      <MultiBinding Converter="{StaticResource multiBooleanConverter}"> 
       <Binding Path="NotPerformingExposure" UpdateSourceTrigger="PropertyChanged"/>Th 
       <Binding Path="NotPerformingFocusTest" UpdateSourceTrigger="PropertyChanged"/> 
      </MultiBinding> 
     </ComboBox.IsEnabled> 
    </ComboBox> 

Cảm ơn! Buck

Trả lời

8

Khi phong cách của bạn kích hoạt, bạn đặt chú giải công cụ thành lỗi Xác thực khi bạn gặp lỗi. Bạn có thể làm điều tương tự khi bạn không có một lỗi bằng cách điều chỉnh Value tài sản của Trigger

<Style x:Key="StandardComboBoxStyle" TargetType="{x:Type ComboBox}"> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="True"> 
      <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
     </Trigger> 
     <Trigger Property="Validation.HasError" Value="False"> 
      <Setter Property="ToolTip" Value="My tooltip test." /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

Ngày lưu ý khác tôi khuyên bạn nên thay đổi Path=(Validation.Errors)[0].ErrorContent để Path=(Validation.Errors).CurrentItem.ErrorContent

+0

phản ứng lớn. Nó hoạt động hoàn hảo - cảm ơn bạn !! – Buck

+0

Giải pháp tuyệt vời, nhưng tôi sẽ làm gì nếu tôi muốn áp dụng điều này cho các hộp kết hợp khác nhau, mỗi hộp có một chú giải công cụ khác nhau. I E. Combobox1 có chú giải công cụ "Tôi là một" và ComboBox2 có chú giải công cụ "Tôi là # 2". Tôi đoán một cái gì đó như nhưng không thể làm cho nó hoạt động! – Dave

Các vấn đề liên quan