使用datatemplate时的wpf绑定

使用datatemplate时的wpf绑定,wpf,xaml,binding,datatemplate,Wpf,Xaml,Binding,Datatemplate,好的,现在我有一段代码: <TabItem Style="{DynamicResource MyStyle" x:Name="TabCustomers" Padding="0,1,4,1" Header={Binding Path=customersHeader}/> 现在我想在那里添加一个图标(通过删除上面的标题): 到目前为止还可以。 我想使用datatemplate来概括这一点。我假设我必须在我的资源字典中这样做: <DataTemplate x:Key="T

好的,现在我有一段代码:

<TabItem Style="{DynamicResource MyStyle" x:Name="TabCustomers" Padding="0,1,4,1"
 Header={Binding Path=customersHeader}/>

现在我想在那里添加一个图标(通过删除上面的标题):


到目前为止还可以。 我想使用datatemplate来概括这一点。我假设我必须在我的资源字典中这样做:

<DataTemplate x:Key="TabItemCustomersTemplate" DataType="{x:Type TabItem}">
<StackPanel Orientation="Horizontal">
     <Image Stretch="UniformToFill" Source="{StaticResource customers}"/>
     <TextBlock x:Key="textblock" Margin="4,0,0,0" 
     Text="{Binding Path=customersHeader}"/>
    </StackPanel>
</DataTemplate>

并在我的tabitem声明中更改此选项:

<TabItem ... HeaderTemplate="{StaticResource TabItemCustomersTemplate}".../>

因此,我遇到了以下问题: 1) 绑定不起作用,为什么? 2) 如何从c#访问文本块? 3) 我怎样才能概括这一点,使我不必为不同的选项卡项(或其他控件)反复复制它,以便每次都能传递我自己的文本和图像源?例如,您可以使用它创建一个图像按钮,如果您有20个按钮,代码就会变得混乱

有什么想法吗

多谢各位

  • 如果您在 tabitem,您不需要设置 模板的数据类型。这个 标题是选项卡的属性 项,它实际上是 键入对象,您可以在其中放入任何内容 在那里

    尝试删除
    DataType=“{x:Type
    TabItem}“
    并查看它是否有效

  • 您不需要访问 文本块从c#开始,您应该 与绑定系统无关。放置 标题中的自定义对象。然后 将此对象绑定到文本块 然后调整对象,它将 操纵文本块。接近 如果元素是硬的,那么它总是硬的 包含在数据模板中。你 应该不需要。如果你发现 你自己走在视觉树上 找到你正在做的视觉元素 困难重重
  • 您可以通过以下方式概括这一点 建议2,使用自定义对象, 删除数据的x:键 模板,并将其数据类型设置为 是自定义对象的类型。 然后无论您的自定义对象在哪里 看来你会得到它的数据 模板正确

  • 试试这个,这个对我有用

    <Window.Resources>
        <!-- <BitmapImage x:Key="customers" UriSource="einstein.jpg"/>--> 
        <DataTemplate x:Key="TabItemCustomersTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Stretch="UniformToFill" Source="{Binding Path=Customers}"/>
                <TextBlock Margin="4,0,0,0" x:Name="txt" Text="{Binding Path=CustomersHeader}"/>
    </StackPanel>
        </DataTemplate>
    </Window.Resources>
        <Grid>
        <TabControl Name="mytabcontrol">
            <TabItem x:Name="TabCustomers" Padding="0,1,4,1" Header="{Binding}" HeaderTemplate="{StaticResource TabItemCustomersTemplate}">
                <Label Content="myContent" Background="Red"/>
            </TabItem>
        </TabControl>
    </Grid>
    
    
    
    暗藏

        public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
    
            var lst = new List<People>();
            lst.Add(new People() { CustomersHeader = "My Customer" });
            this.DataContext = lst;
        }
    }
    
    public class People
    {
        public string CustomersHeader { get; set; }
        public BitmapImage Customers { get; set; }
    }
    
    公共部分类窗口1:窗口
    {
    公共窗口1()
    {
    初始化组件();
    var lst=新列表();
    lst.Add(newpeople(){customerheader=“我的客户”});
    this.DataContext=lst;
    }
    }
    公营阶层人士
    {
    公共字符串customerheader{get;set;}
    公共BitmapImage客户{get;set;}
    }
    
    此外,您还可以使用以下方法在代码隐藏中找到您的文本块

    TabPanel tabPanel = GetVisualChild<TabPanel>(mytabcontrol);
    if (tabPanel != null)
    {
        foreach (UIElement element in tabPanel.Children)
        {
            TabItem tabItem = element as TabItem;
            var image = FindNameFromHeaderTemplate<TextBlock>(tabItem, "txt");
        }
    }
    
        public static T FindNameFromHeaderTemplate<T>(TabItem tabItem, String name) where T : UIElement
        {
            if (tabItem == null)
            {
                throw new ArgumentNullException("container");
            }
    
            if (tabItem.HeaderTemplate == null)
            {
                return null;
            }
    
            ContentPresenter contentPresenter = GetVisualChild<ContentPresenter>(tabItem);
            if (contentPresenter == null)
            {
                return null;
            }
    
            T element = tabItem.HeaderTemplate.FindName(name, contentPresenter) as T;
            return element;
        }
    
        public static T GetVisualChild<T>(Visual referenceVisual) where T : Visual
        {
            Visual child = null;
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
            {
                child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
                if (child != null && child.GetType() == typeof(T))
                {
                    break;
                }
                else if (child != null)
                {
                    child = GetVisualChild<T>(child);
                    if (child != null && child.GetType() == typeof(T))
                    {
                        break;
                    }
                }
            }
            return child as T;
        }
    
    TabPanel TabPanel=GetVisualChild(mytabcontrol);
    if(tabPanel!=null)
    {
    foreach(tabPanel.Children中的UIElement)
    {
    TabItem TabItem=元素作为TabItem;
    var image=FindNameFromHeaderTemplate(tabItem,“txt”);
    }
    }
    公共静态T FindNameFromHeaderTemplate(TabItem TabItem,字符串名称),其中T:UIElement
    {
    if(tabItem==null)
    {
    抛出新的ArgumentNullException(“容器”);
    }
    if(tabItem.HeaderTemplate==null)
    {
    返回null;
    }
    ContentPresenter ContentPresenter=GetVisualChild(tabItem);
    if(contentPresenter==null)
    {
    返回null;
    }
    T element=tabItem.HeaderTemplate.FindName(name,contentPresenter)作为T;
    返回元素;
    }
    公共静态T GetVisualChild(可视引用Visual),其中T:Visual
    {
    可视子对象=空;
    for(Int32 i=0;i
    谢谢!如何在不声明不同数据模板的情况下绑定不同的图像源?您可以在代码隐藏中使用属性并在数据模板中绑定图像的源属性,也可以使用数据模板中的触发器来更改某些特定项的图像。您能演示如何执行第一步吗?在datatemplate的代码中绑定属性?我已经更新了代码,使用数据模板中的绑定和代码隐藏中的属性来显示图像。
    TabPanel tabPanel = GetVisualChild<TabPanel>(mytabcontrol);
    if (tabPanel != null)
    {
        foreach (UIElement element in tabPanel.Children)
        {
            TabItem tabItem = element as TabItem;
            var image = FindNameFromHeaderTemplate<TextBlock>(tabItem, "txt");
        }
    }
    
        public static T FindNameFromHeaderTemplate<T>(TabItem tabItem, String name) where T : UIElement
        {
            if (tabItem == null)
            {
                throw new ArgumentNullException("container");
            }
    
            if (tabItem.HeaderTemplate == null)
            {
                return null;
            }
    
            ContentPresenter contentPresenter = GetVisualChild<ContentPresenter>(tabItem);
            if (contentPresenter == null)
            {
                return null;
            }
    
            T element = tabItem.HeaderTemplate.FindName(name, contentPresenter) as T;
            return element;
        }
    
        public static T GetVisualChild<T>(Visual referenceVisual) where T : Visual
        {
            Visual child = null;
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
            {
                child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
                if (child != null && child.GetType() == typeof(T))
                {
                    break;
                }
                else if (child != null)
                {
                    child = GetVisualChild<T>(child);
                    if (child != null && child.GetType() == typeof(T))
                    {
                        break;
                    }
                }
            }
            return child as T;
        }