2011-08-24 80 views
5

我有一个项目,我希望能够有一些控件的工具提示,这些控件将包含一些控件,如文本框和日期选择器。这个想法是有一些有限的图形弹出式窗口,但有一些控制互动。WPF工具提示与控件

我知道如何向控件添加“正常”工具提示,但是当您移动时,工具提示会消失,因此我无法与其进行交互。

这是可能的吗?如果是的话,如果没有,有没有其他办法呢?

感谢

回答

10

您应该使用Popup代替ToolTip

例。当在TextBox鼠标移动和停留,只要鼠标在TextBoxPopup

<TextBox Name="textBox" 
     Text="Popup On Mouse Over" 
     HorizontalAlignment="Left"/> 
<Popup PlacementTarget="{Binding ElementName=textBox}" 
     Placement="Bottom"> 
    <Popup.IsOpen> 
     <MultiBinding Mode="OneWay" Converter="{StaticResource BooleanOrConverter}"> 
      <Binding Mode="OneWay" ElementName="textBox" Path="IsMouseOver"/> 
      <Binding RelativeSource="{RelativeSource Self}" Path="IsMouseOver" /> 
     </MultiBinding> 
    </Popup.IsOpen> 
    <StackPanel> 
     <TextBox Text="Some Text.."/> 
     <DatePicker/> 
    </StackPanel> 
</Popup> 

公开赛是使用BooleanOrConverter

public class BooleanOrConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     foreach (object booleanValue in values) 
     { 
      if (booleanValue is bool == false) 
      { 
       throw new ApplicationException("BooleanOrConverter only accepts boolean as datatype"); 
      } 
      if ((bool)booleanValue == true) 
      { 
       return true; 
      } 
     } 
     return false; 
    } 
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

更新
给一个Popup被打开为DataGrid中的单元格执行此操作,您有几个选项。其中两个是在DataTemplates内部添加PopupDataGridTemplateColumn,或者您可以将其添加到DataGridCell Template。这是后面的例子。它会要求你在DataGrid

<DataGrid SelectionMode="Single" 
      SelectionUnit="Cell" 
      ...> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type DataGridCell}"> 
         <Grid> 
          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
           <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </Border> 
          <Popup PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" 
            Placement="Right" 
            IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"> 
           <StackPanel> 
            <TextBox Text="Some Text.."/> 
            <DatePicker/> 
           </StackPanel> 
          </Popup> 
         </Grid>         
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style>  
    </DataGrid.CellStyle> 
    <!--...--> 
</DataGrid> 
+0

设置的SelectionMode =“单”和SelectionUnit =“细胞”这个问题能在DatagridRow完成,或者我应该使用TemplateColumn中与文本块呢? –

+0

当然,你想如何工作? “Popup”何时应该可见等? –

+0

我想在选中单元格时打开弹出窗口。完成后,带有选项的小型弹出窗口将可用。 –