简体   繁体   English

如果文本框包含文本WPF,则在tabcontrol选项卡标题中显示图像

[英]show image in tabcontrol tab header if textbox has text WPF

Okay, What I'm trying to accomplish: 好的,我要完成的工作:

A tabheader which gets an image if the textbox inside has text. 如果其中的文本框包含文本,则获取图像的Tabheader。 but if the textbox inside the TabItem doesn't have any text, then the image should not be shown. 但是如果TabItem内的文本框没有任何文本,则不应显示该图像。

this is what I have so far: 这是我到目前为止所拥有的:

----- TAB ITEM CODE ----- -----标签项目代码-----

            <TabItem Name="tabAantekeningen"  Header="">
                <TabItem.HeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Margin="8" Text="Aantekeningen"/>
                            <Image Grid.Column="1" Source="..\Resources\validate.png" Height="20" Width="17"/>
                        </Grid>
                    </DataTemplate>
                </TabItem.HeaderTemplate>
                <TextBox Name="txtOmschrijving" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
            </TabItem>

----- TAB ITEM CODE ----- -----标签项目代码-----

----- Code Behind ----- -----后面的代码-----

    public void SetTabItemHeader()
    {
        if (String.IsNullOrEmpty(txtOmschrijving.Text))
        {
            tabAantekeningen.Header = "Aantekeningen";
        }
    }

----- Code Behind ----- -----后面的代码-----

IS there a way that I can say: txtOmschrijving.Text == Empty so hide the Image? 有没有办法说:txtOmschrijving.Text ==空,所以隐藏图像?

Edit: Didn't see your seccond question there, yes there is use a IValueConverter , where you check if the string is empty and Bind To Visibility for instance, so you return Visbility.Collapsed when empty or else Visbility.Visible. 编辑:那里没有看到您的第二个问题,是的,使用了IValueConverter ,您在其中检查字符串是否为空,并且例如绑定到Visibility,因此在为空时返回Visbility.Collapsed,否则返回Visbility.Visible。 Like this : 像这样 :

public class StringEmptyToVisbililityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value == null) || !(value is string) || string.IsNullOrEmpty(value.ToString()) ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Fully working xaml, change your namespaces and URI pack 可以正常使用的XAML,更改您的名称空间和URI包

<Window x:Class="TabItemHeader.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:local="clr-namespace:TabItemHeader"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:StringEmptyToVisbililityConverter x:Key="StringEmptyToVisbililityConverter"/>
</Window.Resources>
<Grid>        
    <TabControl>
        <TabItem Name="tabAantekeningen">
            <TabItem.HeaderTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Margin="8" Text="{Binding Path='Header',RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"/>
                        <Image Grid.Column="1" Source="pack://application:,,,/TabItemHeader;component/Resources/Images/validate.png" Height="20" Width="17" Visibility="{Binding Path='Header', Converter={StaticResource StringEmptyToVisbililityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"/>
                    </Grid>
                </DataTemplate>
            </TabItem.HeaderTemplate>
            <TextBox Name="txtOmschrijving" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
        </TabItem>
    </TabControl>
</Grid>

This will bind to the listboxitem which wraps everything in a listbox. 这将绑定到将所有内容包装在列表框中的listboxitem。 The converter will only show this image when the string is not empty. 仅当字符串不为空时,转换器才会显示此图像。 You can do alot of fun with them :) 您可以和他们一起做很多有趣的事情:)

WPF cheat sheet is a really handy and compact paper on all types of bindings. WPF备忘单是用于所有类型装订的非常方便且紧凑的纸张。

Oh..I am assuming this image will be deployed with your application? 哦..我假设此映像将与您的应用程序一起部署? Then please ensure that your image is set to resource, you should consider using uri packs as well for your images, an example is in this post as well as the xaml provided. 然后,请确保将您的图像设置为资源,您应该考虑对图像也使用uri包 ,本文中的示例以及提供的xaml都作为示例 If your image is dynamic, you will have to bind them to some model in an observablecollection. 如果图像是动态的,则必须将它们绑定到可观察的集合中的某个模型。 Tip: I'll stop pushing this to far, but you should consider having a look at the MVVM pattern. 提示:我将不再对此进行过多介绍,但是您应该考虑看看MVVM模式。 I just used code behind my self, so the answer wouldn't get to big. 我只是在自己的背后使用代码,所以答案不会太大。 It's whole other topic! 这是另一个主题! =) There are also cleaner ways to either share templates, and change them on types bound in the collection. =)还有一些更干净的方法可以共享模板,并根据集合中绑定的类型更改模板。

Hope it helps. 希望能帮助到你。

Cheers, 干杯,

Stian 了Stian

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM