【问题标题】:Binding Failure on ToolBar Dragging工具栏拖动绑定失败
【发布时间】:2014-10-11 13:00:19
【问题描述】:

我有一个ToolBar,我正在使用下面显示的代码对其进行样式设置。我的ToolBar 样式遇到了三个问题:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:cal="http://www.caliburnproject.org">
    <Style x:Key="ToolBarToggleButton" TargetType="{x:Type ToggleButton}" 
           BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
        <Setter Property="Icon" Value="{Binding Icon}"/>
        <Setter Property="ToolTip" Value="{Binding FullToolTip}" />
        <Setter Property="ToolTipService.IsEnabled" Value="{Binding HasToolTip}" />
        <Setter Property="IsChecked" Value="{Binding IsChecked}" />
        <Setter Property="cal:Action.Target" Value="{Binding}" />
        <Setter Property="cal:Message.Attach" Value="{Binding ActionText}" />
    </Style>
    ...

第一:

工具栏图像目前被用作单个静态实例。即使使用属性x:Shared="False" 将ToolBar 移到另一个上也会导致渲染失败。

解决方案:

在 ToolTip Styles.xaml 中,将图像源包含为 ContentTemplate

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:cal="http://www.caliburnproject.org">
    <Style x:Key="ToolBarToggleButton" TargetType="{x:Type ToggleButton}" 
           BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Image x:Shared="false" Source="{Binding Icon}">
                        <Image.Style>
                            <Style TargetType="Image">
                                <Setter Property="Width" Value="16" />
                                <Setter Property="Height" Value="16" />
                                <Setter Property="Stretch" Value="Fill" />
                                <Style.Triggers>
                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="Opacity" Value="0.5" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ToolTip" Value="{Binding FullToolTip}" />
        <Setter Property="ToolTipService.IsEnabled" Value="{Binding HasToolTip}" />
        <Setter Property="IsChecked" Value="{Binding IsChecked}" />
        <Setter Property="cal:Action.Target" Value="{Binding}" />
        <Setter Property="cal:Message.Attach" Value="{Binding ActionText}" />
    </Style>

    <Style x:Key="ToolBarButton" TargetType="{x:Type Button}" 
           BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Image x:Shared="false" Source="{Binding Icon}">
                        <Image.Style>
                            <Style TargetType="Image">
                                <Setter Property="Width" Value="16" />
                                <Setter Property="Height" Value="16" />
                                <Setter Property="Stretch" Value="Fill" />
                                <Style.Triggers>
                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="Opacity" Value="0.5" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ToolTip" Value="{Binding FullToolTip}" />
        <Setter Property="ToolTipService.IsEnabled" Value="{Binding HasToolTip}" />
        <Setter Property="cal:Action.Target" Value="{Binding}" />
        <Setter Property="cal:Message.Attach" Value="{Binding ActionText}" />
    </Style>
</ResourceDictionary>

现在我们有了:

无需其他更改。

第二:

ToolTip 和 ToolTipService 绑定的相同拖动过程也会损坏 - 在这个阶段我不确定根本原因。快速的解决方案是按照我对上图所做的操作,并将ToolTip 添加到Image。但是,这会导致 ToolTip 仅在 Image 上方时显示,而不是在将鼠标悬停在按钮的最边缘时显示。

部分解决方案:

将ToolTip 添加到Image。但是,这并不理想,因为工具提示在按钮的最边缘时不会显示。

第三:

当发生如上所示的拖动操作时,这也会破坏 Caliburn 属性绑定

<Setter Property="cal:Action.Target" Value="{Binding}" />
<Setter Property="cal:Message.Attach" Value="{Binding ActionText}" />

问题:(第二和第三个问题)

  1. 如何解决ToolTip 渲染的问题?
  2. 如何解决 Caliburn 属性绑定的问题?

感谢您的宝贵时间。


解决方案:

“我终于解决了这个问题。据我所知,当您在工具栏上启动拖动时,您拖动的工具栏所覆盖的其他工具栏上的任何工具栏项目都会从当前位置删除视觉树,然后(可能)在它们再次可见时添加回来。

当它们从可视化树中移除时(再次,我认为这是正在发生的情况),它们也无法访问 ToolBarToggleButton 和 ToolBarButton 样式,因为它们是在添加到 ToolBarsView 的 Styles.xaml 资源字典中定义的.xaml。

但是,如果您在全局范围内定义这些相同的样式,那么它就可以工作 - 大概,即使它们不再是 ToolBarsView 的子项,它们仍然可以访问全局资源。

所以...我添加了一个新属性 IModule.GlobalResourceDictionaries,它允许每个模块声明应在全局范围内添加的任何资源字典。 ToolBars 模块利用了这一点。”

https://github.com/tgjones/gemini/issues/67#issuecomment-60040008

【问题讨论】:

  • 看起来一些自定义样式/模板正在应用于这些工具栏——颜色方案看起来不像来自系统主题。您是否尝试过剥离自定义样式以查看是否有帮助?
  • 嗨,迈克,非常感谢您的回复。我将在今晚晚些时候确认这一点。我目前不在办公桌前。再次感谢。
  • 你的描述说你卖掉了你的sole,这是soul上的一点英式幽默。 ;-)
  • 我喜欢发展,但数学/物理是我的最爱。虽然没有钱......我现在在业余时间将这三个结合起来!

标签: c# wpf mvvm binding caliburn.micro


【解决方案1】:

尝试将工具提示重新分配为鼠标悬停事件的样式触发器,例如:

    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                   <Setter Property="ToolTip" Value="{Binding FullToolTip}" />
                   <Setter Property="ToolTipService.IsEnabled" Value="{Binding HasToolTip}" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="False">
                   <Setter Property="ToolTip" Value="{Binding FullToolTip}" />
                   <Setter Property="ToolTipService.IsEnabled" Value="{Binding HasToolTip}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>

【讨论】:

  • 这看起来很有希望!我现在就试试这个……对 Caliburn 绑定有什么想法吗?
  • 效果很好。如果您可以为第三个问题提供解决方案,那么赏金就是您的先生。一切顺利。
  • @Killercam 不清楚(至少对我而言)Caliburn 绑定在这种特定情况下失败的原因。你能从等式中删除 Caliburn 并以通用方式表示问题吗?这可以提供两个好处,1)它可以定位问题并提供对失败的洞察力,2)为像我这样的人提供一个基本的例子来修补......
  • @Killercam Caliburn 问题怎么样?...希望您能够解决它。
猜你喜欢
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 2012-11-23
相关资源
最近更新 更多