工具提示和ContentTemplateSelector的多绑定

时间:2018-04-10 02:26:07

标签: wpf data-binding tooltip multibinding datatemplateselector

如何将工具提示和ContentTemplateSelector多个绑定到两个或多个属性?这是骨架代码:

<Grid>
     <Grid Definitions>
      <Content Control
          **Line A** *ToolTip*//Want to bind to the same two properties as in the Text Box below and invoke the converter
          **Line B** *ContentTemplateSelector*  //Want to bind to the same two properties as in the Text Box below and invoke the converter
       />  
       **Line C** <TextBlock
          <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource CountToStringConverter}">
                            <Binding Path="Property1"/>
                            <Binding Path="Property2" />
                        </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
<Grid Ends>

看起来我不能使用我用于文本块的MultiBinding方式。我的ContentTemplateSelector转换器继承了DataTemplateSelector控件。任何变通方法/输入值得赞赏。另外,我无法对工具提示使用类似的多重绑定声明

2 个答案:

答案 0 :(得分:1)

关于问题的第一部分,您可以像设置文本块一样设置height的{​​{1}},如果 `.maincontent { margin-top: 200px; background: #E0E0E0; float: left; border: 1px solid #013A57; padding: 3px 2px; margin-top: 0px; border-radius: 5px; height: 750pxmain; width: 900px; margin-left: 15px; height: 750px; } .maincontent table{ height: 20%; overflow-y: scroll; width: 55%; margin-top: 30px; margin-left: 30px } .maincontent tr{ background: #E0E0E0; border-bottom: 1px solid #FFF; margin-bottom: 5px; } .maincontent td{ font-family: 'Lato', sans-serif; font-weight: bold; padding: 10px; text-align: left; width: 33.3333%; font-size: 12px; } .maincontent h3{ padding: 10px; border-bottom: 2px solid #013A57; margin: 10px; font-size: 23px; color: #013A57; }` <div class="maincontent" style=""> <h3>Register Here</h3> <div class="row justify-content-center" style="float: left;margin-left: 2px;margin-top: 50px"> <div class="col" style="width: 200px"> <?php if(!empty($msg)) { ?> div id="dialog" title="" style="background: #013A57; color: white;"> <?php echo $msg;?> </div> <?php }?> <form method="post" action="register.php" enctype="multipart/form- data" style="width: 860px;margin: 0px;height: 580px;"> <input class="form-control" type="" name="name" placeholder="Name..." style="width: 400px; margin-left: 200px" ><br> <input class="form-control" type="email" name="email" placeholder="Email..." style="width: 400px; margin-left: 200px" ><br> <input class="form-control" type="password" name="password" placeholder="Password" style="width: 400px; margin-left: 200px" ><br> <input class="form-control" type="password" name="cPassword" placeholder="confirm Password..." style="width: 400px; margin-left: 200px" ><br> <input class="form-control" type="text" name="contactNO" placeholder="Your contact No.." style="width: 400px; margin-left: 200px" ><br> <div class="radio"> <h6>Select a Category</h6> <input type="radio" name="category" value="customer">Customer<br> <input type="radio" name="category" value="DispensaryOwner">Dispensary Owner<br> <input type="radio" name="category" value="dealer">Dealer<br> </div> <br> <div class="address radio" > <h6 style="margin-left: 20px">Address</h6> <select name="district" id="district"> <option value="">Select District</option> <?php echo load_district(); ?> </select> <br> <select name="place" id="place"> <option value="">Select place</option> </select> </div> <br> <input class="btn-primary" type="submit" name="submit" value="Register" style=" margin-left: 200px"><br><br> </form> </div> </div> </div>为空则工具提示赢了&#39 ; t出现。

对于ToolTip,只需定义两个使用ContentControl和转换器的模板(确保使用ContentControl绑定属性以避免TemplateSelector方案),并将Multibinding的{​​{1}}绑定到使用两个定义的ElementName设置的UnserValue,以下是如何执行此操作的完整示例:

MainWindow xaml:

ContentTemplateSelector

TemplateSelector,Converter和Codebehind:

ContentControl

<强>更新

正如我所说,使用DataTriggers更容易做到这一点,因此为每个图标定义DataTemplateSelector,并根据DataTemplates的值设置<Window ... x:Name="Main" Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Self}}" > <Window.Resources> <local:CountToStringConverter x:Key="CountToStringConverter"/> <DataTemplate x:Key="FirstTemplate"> <Grid> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource CountToStringConverter}"> <Binding Path="DataContext.Property1" ElementName="Main"/> <Binding Path="DataContext.Property2" ElementName="Main"/> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid> </DataTemplate> <DataTemplate x:Key="SecondTemplate"> <Grid> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource CountToStringConverter}"> <Binding Path="DataContext.Property2" ElementName="Main"/> <Binding Path="DataContext.Property1" ElementName="Main"/> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid> </DataTemplate> <local:MyTemplateSelector x:Key="MyTemplateSelector" FirstDataTemplate="{StaticResource FirstTemplate}" SecondDataTemplate="{StaticResource SecondTemplate}"/> </Window.Resources> <Grid> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ContentControl ContentTemplateSelector="{StaticResource MyTemplateSelector}"> <ContentControl.ToolTip> <MultiBinding Converter="{StaticResource CountToStringConverter}"> <Binding Path="Property1"/> <Binding Path="Property2" /> </MultiBinding> </ContentControl.ToolTip> </ContentControl> <TextBlock Grid.Row="1"> <TextBlock.Text> <MultiBinding Converter="{StaticResource CountToStringConverter}"> <Binding Path="Property1"/> <Binding Path="Property2" /> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid> </Grid> </Window> public class MyTemplateSelector : DataTemplateSelector { public DataTemplate FirstDataTemplate { get; set; } public DataTemplate SecondDataTemplate { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { // select your template based on item return (new Random()).Next(2)==0?SecondDataTemplate:FirstDataTemplate; } } public class CountToStringConverter:IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values == null) return null; return values[0]?.ToString() + values[1]?.ToString(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public partial class MainWindow : Window, INotifyPropertyChanged { private string _property1; public string Property1 { get { return _property1; } set { if (value == _property1) return; _property1 = value; OnPropertyChanged(); } } private string _property2; public string Property2 { get { return _property2; } set { if (value == _property2) return; _property2 = value; OnPropertyChanged(); } } public MainWindow() { InitializeComponent(); Property1 = "Property 1"; Property2 = "Property 2"; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 使用相同的多值转换器:

让我们假设您有两个DataTemplates定义如下:

DataTrigger

您的DataTriggers应如下所示:

ContentTemplate

现在,如果要绑定图标,可以使用property1绑定始终执行此操作:

property2

您可能还会考虑的另一个选项是在<Window.Resources> <DataTemplate x:Key="WarningIconImageTemplate"> <StackPanel> <TextBlock Text="Some Text"></TextBlock> <Image Source="/Icons/warningIcon.png"></Image> </StackPanel> </DataTemplate> <DataTemplate x:Key="CautionIconImageTemplate"> <StackPanel> <TextBlock Text="Some Other Text"></TextBlock> <Image Source="/Icons/cautionIcon.png"></Image> </StackPanel> </DataTemplate> <local:CountToStringConverter x:Key="CountToStringConverter"/> </Window.Resources> 中定义<ContentControl> <ContentControl.Style> <Style TargetType="ContentControl"> <Style.Triggers> <DataTrigger > <DataTrigger.Binding> <MultiBinding Converter="{StaticResource CountToStringConverter}"> <Binding Path="Property1"/> <Binding Path="Property2" /> </MultiBinding> </DataTrigger.Binding> <DataTrigger.Value> <system:String>Property 1Property 2</system:String> <!--update based on your need--> </DataTrigger.Value> <Setter Property="ContentTemplate" Value="{StaticResource WarningIconImageTemplate}"/> <Setter Property="ToolTip" Value="First ToolTip"/> </DataTrigger> <DataTrigger > <DataTrigger.Binding> <MultiBinding Converter="{StaticResource CountToStringConverter}"> <Binding Path="Property1"/> <Binding Path="Property2" /> </MultiBinding> </DataTrigger.Binding> <DataTrigger.Value> <system:String>Property 2Property 1</system:String> <!--update based on your need--> </DataTrigger.Value> <Setter Property="ContentTemplate" Value="{StaticResource CautionIconImageTemplate}"/> <Setter Property="ToolTip" Value="Second ToolTip"/> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl>

答案 1 :(得分:0)

您可以通过以下两种方式之一编写任何属性:

或者:

<ContentControl ToolTip="Some Tooltip" />

<ContentControl>
    <ContentControl.ToolTip>
        <MultiBinding Converter="{StaticResource CountToStringConverter}">
            <Binding Path="Property1"/>
            <Binding Path="Property2" />
        </MultiBinding>
    </ContentControl.ToolTip>
</ContentControl>