我如何使用的ElementName一个控件模板内的绑定? [英] How can I use an ElementName binding within a ControlTemplate?

查看:124
本文介绍了我如何使用的ElementName一个控件模板内的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有引用我的应用程序不同元素多的TextBlocks。直接在页面中使用时,我的code正常工作。不过,我想创建一个的ControlTemplate和一个ContentControl中减少code的重复。

我如何传递一个引用到的ElementName到使用TemplateBinding从ContentControl中的控件模板?下面code抛出这个错误:


  

无法在属性'的ElementName'的值转换为类型的对象
  System.String。种类型的目标
  System.Windows.TemplateBindingEx pression无法转换为类型
  System.String。


除了标签属性,我已经试过ContentStringFormat也没有工作。

是什么让这个使用模板来工作的正确方法是什么?

在此先感谢您的帮助,

---肖恩

下面是code样品:<​​/ P>

 &LT;页的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation的xmlns:SYS =CLR的命名空间:系统;装配= mscorlib程序的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml&GT;
    &LT; Page.Resources&GT;
        &LT;控件模板X:键=MyTemplate的的TargetType ={X:类型ContentControl中}&GT;
            &LT; TextBlock的保证金={绑定的ElementName = {TemplateBinding标签},路径=保证金}文本={TemplateBinding内容}TextAlignment ={绑定的ElementName = {TemplateBinding标签},路径= TextAlignment}WIDTH ={结合的ElementName = {TemplateBinding标签},路径=宽度}/&GT;
        &LT; /控件模板&GT;
    &LT; /Page.Resources>
    &LT;&StackPanel的GT;
        &LT; TextBlock的X:名称=AnotherElement保证金=10文本=主TextBlock的TextAlignment =中心WIDTH =100/&GT;
        &LT; TextBlock的X:名称=AnotherElement2保证金=20文本=第二TextBlock的TextAlignment =左WIDTH =250/&GT;
        &LT; TextBlock的保证金={绑定的ElementName = AnotherElement,路径=保证金}文本=这里是我的TextBlock! TextAlignment ={绑定的ElementName = AnotherElement,路径= TextAlignment}TextTrimming =CharacterEllipsisWIDTH ={绑定的ElementName = AnotherElement,路径=宽度}/&GT;
        &LT; TextBlock的保证金={绑定的ElementName = AnotherElement2,路径=保证金}文本=这里是我的第二TextBlock的! TextAlignment ={绑定的ElementName = AnotherElement2,路径= TextAlignment}TextTrimming =CharacterEllipsisWIDTH ={绑定的ElementName = AnotherElement2,路径=宽度}/&GT;
        &LT; ContentControl中CONTENT =你好!标签=AnotherElement模板={StaticResource的MyTemplate的}/&GT;
        &LT; ContentControl中CONTENT =你好!再次标签=AnotherElement2模板={StaticResource的MyTemplate的}/&GT;
    &LT; / StackPanel的&GT;
&LT; /页&GT;


解决方案

这似乎是一个有趣的方式到模板的东西,但它可以做到的,你只需要得到一点幻想与你绑定。

下面的工作,但我仍然不认为这是一个模板控制的好办法

绑定的TextBlock 标签来实际的元素,然后在控件模板绑定标签标签并从那里作为标签的元素,你可以使用使用的值从它的任何元素。

 &LT; Page.Resources&GT;
    &LT;控件模板X:键=MyTemplate的的TargetType ={X:类型ContentControl中}&GT;
        &LT; TextBlock的名称=_这个标签={TemplateBinding标签}保证金={绑定的ElementName = _this,路径= Tag.Margin}文本={TemplateBinding内容}TextAlignment ={绑定的ElementName = _this,路径= Tag.TextAlignment}WIDTH ={绑定的ElementName = _this,路径= Tag.Width}/&GT;
    &LT; /控件模板&GT;
&LT; /Page.Resources>
&LT;&StackPanel的GT;
    &LT; TextBlock的X:名称=AnotherElement保证金=10文本=主TextBlock的TextAlignment =中心WIDTH =100/&GT;
    &LT; TextBlock的X:名称=AnotherElement2保证金=20文本=第二TextBlock的TextAlignment =左WIDTH =250/&GT;
    &LT; TextBlock的保证金={绑定的ElementName = AnotherElement,路径=保证金}文本=这里是我的TextBlock! TextAlignment ={绑定的ElementName = AnotherElement,路径= TextAlignment}TextTrimming =CharacterEllipsisWIDTH ={绑定的ElementName = AnotherElement,路径=宽度}/&GT;
    &LT; TextBlock的保证金={绑定的ElementName = AnotherElement2,路径=保证金}文本=这里是我的第二TextBlock的! TextAlignment ={绑定的ElementName = AnotherElement2,路径= TextAlignment}TextTrimming =CharacterEllipsisWIDTH ={绑定的ElementName = AnotherElement2,路径=宽度}/&GT;
    &LT; ContentControl中CONTENT =你好!标签={绑定的ElementName = AnotherElement}模板={StaticResource的MyTemplate的}/&GT;
    &LT; ContentControl中CONTENT =你好!再次标签={绑定的ElementName = AnotherElement2}模板={StaticResource的MyTemplate的}/&GT;
&LT; / StackPanel的&GT;

I have multiple TextBlocks which reference different elements in my application. My code works fine when used directly in the page. However, I want to create a ControlTemplate and a ContentControl to reduce the duplication of code.

How can I pass a reference to an ElementName into the ControlTemplate from the ContentControl using TemplateBinding? The following code throws this error:

"Cannot convert the value in attribute 'ElementName' to object of type 'System.String'. Object of type 'System.Windows.TemplateBindingExpression' cannot be converted to type 'System.String'. "

In addition to the Tag attribute, I have tried ContentStringFormat which also did not work.

What is the correct method to get this to work using templates?

Thanks in advance for your help,

--- Shawn

Here is the code sample:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Page.Resources>
        <ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
            <TextBlock Margin="{Binding ElementName={TemplateBinding Tag}, Path=Margin}" Text="{TemplateBinding Content}" TextAlignment="{Binding ElementName={TemplateBinding Tag}, Path=TextAlignment}" Width="{Binding ElementName={TemplateBinding Tag}, Path=Width}" />
        </ControlTemplate>
    </Page.Resources>
    <StackPanel>
        <TextBlock x:Name="AnotherElement" Margin="10" Text="Main TextBlock" TextAlignment="Center" Width="100" />
        <TextBlock x:Name="AnotherElement2" Margin="20" Text="Second TextBlock" TextAlignment="Left" Width="250" />
        <TextBlock Margin="{Binding ElementName=AnotherElement, Path=Margin}" Text="Here is my TextBlock!" TextAlignment="{Binding ElementName=AnotherElement, Path=TextAlignment}" TextTrimming="CharacterEllipsis" Width="{Binding ElementName=AnotherElement, Path=Width}" />
        <TextBlock Margin="{Binding ElementName=AnotherElement2, Path=Margin}" Text="Here is my Second TextBlock!" TextAlignment="{Binding ElementName=AnotherElement2, Path=TextAlignment}" TextTrimming="CharacterEllipsis" Width="{Binding ElementName=AnotherElement2, Path=Width}" />
        <ContentControl Content="Hello!" Tag="AnotherElement" Template="{StaticResource MyTemplate}" />
        <ContentControl Content="Hello Again!" Tag="AnotherElement2" Template="{StaticResource MyTemplate}" />
    </StackPanel>
</Page>

解决方案

This seems like a funny way to template something, but it can be done, you just have to get a bit fancy with your bindings.

The below will work, but I still dont think this is a good way to template a control

Bind the TextBlock Tag to the actual Element, then in the ControlTemplate bind Tag to Tag and use the values from there as Tag is the Element, you can use any element from it.

<Page.Resources>
    <ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
        <TextBlock Name="_this" Tag="{TemplateBinding Tag}" Margin="{Binding ElementName=_this, Path=Tag.Margin}" Text="{TemplateBinding Content}" TextAlignment="{Binding ElementName=_this, Path=Tag.TextAlignment}" Width="{Binding ElementName=_this, Path=Tag.Width}" />
    </ControlTemplate>
</Page.Resources>
<StackPanel>
    <TextBlock x:Name="AnotherElement" Margin="10" Text="Main TextBlock" TextAlignment="Center" Width="100" />
    <TextBlock x:Name="AnotherElement2" Margin="20" Text="Second TextBlock" TextAlignment="Left" Width="250" />
    <TextBlock Margin="{Binding ElementName=AnotherElement, Path=Margin}" Text="Here is my TextBlock!" TextAlignment="{Binding ElementName=AnotherElement, Path=TextAlignment}" TextTrimming="CharacterEllipsis" Width="{Binding ElementName=AnotherElement, Path=Width}" />
    <TextBlock Margin="{Binding ElementName=AnotherElement2, Path=Margin}" Text="Here is my Second TextBlock!" TextAlignment="{Binding ElementName=AnotherElement2, Path=TextAlignment}" TextTrimming="CharacterEllipsis" Width="{Binding ElementName=AnotherElement2, Path=Width}" />
    <ContentControl Content="Hello!" Tag="{Binding ElementName=AnotherElement}" Template="{StaticResource MyTemplate}" />
    <ContentControl Content="Hello Again!" Tag="{Binding ElementName=AnotherElement2}" Template="{StaticResource MyTemplate}" />
</StackPanel>

这篇关于我如何使用的ElementName一个控件模板内的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆