【问题标题】:Bind to child element of content template by name from outside从外部按名称绑定到内容模板的子元素
【发布时间】:2014-08-21 20:39:50
【问题描述】:

我怎样才能从外部ConnectingLine(绑定到FrameworkElements 并用一条线连接它们的自定义控件)到名为“Top”和“Bottom”的内部TextBlocks 的绑定工作?请注意,我想要整个FrameworkElements 来获取位置信息。

<Grid>
    <ConnectingLine From="{Binding ElementName=Button1.Top}" To="{Binding ElementName=Button2.Top}" />
    <ConnectingLine From="{Binding ElementName=Button1.Bottom}" To="{Binding ElementName=Button2.Bottom}" />
    <ToggleButton x:Name="Button1">
        <ToggleButton.Template>
            <ControlTemplate>
                <Grid>
                    <Rectangle x:Name="Top" />
                    <Rectangle x:Name="Bottom" />
                </Grid>
            </ControlTemplate>
        </ToggleButton.Template>
    </ToggleButton>
    <ToggleButton x:Name="Button2">
        <ToggleButton.Template>
            <ControlTemplate>
                <Grid>
                    <Rectangle x:Name="Top" />
                    <Rectangle x:Name="Bottom" />
                </Grid>
            </ControlTemplate>
        </ToggleButton.Template>
    </ToggleButton>
</Grid>

我的目标是能够从 XAML 中进行绑定。理想情况下没有多余的绒毛,但涉及自定义绑定运算符或附加属性的解决方案可能是可以接受的。

编辑: 我希望如何输出:

每个不同颜色的列都是模板化的ToggleButtons 之一,在顶部和底部元素之间已经有一个虚线ConnectingLine。水平实线是我感兴趣的。目前我正在通过代码隐藏实现我想要的。

【问题讨论】:

  • 如果您覆盖默认模板,您将无法从 XAML 访问它。绑定到 TextBlock 有什么意义?
  • 我想绑定到它,这样我就可以在两个 TextBlocks 之间画一条线,当 TextBlocks 移动时,它会适应它的起点和终点。
  • 对不起,我不明白,但大概有更好的出路。
  • 我再次编辑了它。不过感谢您的努力。
  • 你想画一条线,你不能在矩形之间做吗?

标签: c# wpf data-binding controltemplate


【解决方案1】:
<ToggleButton x:Name="Button">
    <ToggleButton.Template>
        <ControlTemplate>
            <Grid>
               <CheckBox x:Name="FindMe" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked}"/>
            </Grid>
        </ControlTemplate>
    </ToggleButton.Template>
</ToggleButton>

让我知道它是否有效。

【讨论】:

  • 我应该编辑我的问题。我实际上需要挖掘整个控件,所以在我的情况下没有Path,只有{Binding ElementName=...}。我正在使用名称,以便区分相同类型的 2 个控件。
  • 我不明白,这个解决方案有什么问题?更准确地解释你想要获得什么。
  • 我不认为有什么问题,但我的问题是不恰当的。我会在一分钟内编辑它。
猜你喜欢
  • 1970-01-01
  • 2013-10-28
  • 2015-03-26
  • 1970-01-01
  • 2018-10-21
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多