首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tooltip和ContentTemplateSelector的多重绑定

tooltip和ContentTemplateSelector的多重绑定
EN

Stack Overflow用户
提问于 2018-04-10 10:26:07
回答 2查看 598关注 0票数 0

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

代码语言:javascript
复制
<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控件.Any的变通方法/输入。此外,我无法对工具提示使用类似的多绑定声明

EN

回答 2

Stack Overflow用户

发布于 2018-04-10 18:12:06

关于问题的第一部分,您可以像设置文本块一样设置ContentControlToolTip,如果ContentControl为空,则不会显示工具提示。

对于TemplateSelector,只需定义两个使用Multibinding和转换器的模板(确保使用ElementName绑定属性以避免UnserValue场景),并将ContentControlContentTemplateSelector绑定到使用这两个定义的DataTemplates设置的DataTemplateSelector,下面是如何完成此操作的完整示例:

MainWindow xaml:

代码语言:javascript
复制
<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>

TemplateSelector、转换器和代码背后:

代码语言:javascript
复制
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));
    }

}

更新

正如我所说的,使用DataTriggers应该更容易做到这一点,所以为每个图标定义一个DataTrigger,并使用相同的多值转换器根据property1property2的值设置ContentTemplate

假设你有两个这样定义的DataTemplates:

代码语言:javascript
复制
<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>

您的DataTriggers应该如下所示:

代码语言:javascript
复制
<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>

现在,如果你想绑定图标,你可以使用ElementName绑定:

代码语言:javascript
复制
<Window.Resources>
    <DataTemplate x:Key="WarningIconImageTemplate">
        <StackPanel>
            <TextBlock Text="Some Text"></TextBlock>
            <Image Source="{Binding DataContext.WarningImageSource,ElementName=Main}" ></Image>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

您也可以考虑的另一个选择是在IconImageTemplate中定义DataTrigger

票数 1
EN

Stack Overflow用户

发布于 2018-04-10 13:35:56

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

以下任一项:

代码语言:javascript
复制
<ContentControl ToolTip="Some Tooltip" />

代码语言:javascript
复制
<ContentControl>
    <ContentControl.ToolTip>
        <MultiBinding Converter="{StaticResource CountToStringConverter}">
            <Binding Path="Property1"/>
            <Binding Path="Property2" />
        </MultiBinding>
    </ContentControl.ToolTip>
</ContentControl>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49744362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档