简体   繁体   English

将图片显示为隐藏或可见

[英]Display pictures as hide or visible

Goal:目标:
When user start typing text or characters in the textbox txtSearch the picture picEnlarger will be hidden and be replaced by picture picXmark.当用户开始在文本框 txtSearch 中输入文本或字符时,图片 picEnlarger 将被隐藏并替换为图片 picXmark。 In default, the picEnlarger will always display until input data will be applied in the textbox txtSearch.默认情况下,picEnlarger 将始终显示,直到输入数据应用到文本框 txtSearch。 In order word, no data in textbox then display picEnlarger and hide picXmark.换句话说,文本框中没有数据,然后显示 picEnlarger 并隐藏 picXmark。

Problem:问题:
Having problem to display the picture picXmark and hide the picture picEnlarger when the user start typing characters in the textbox named txtSearch.当用户开始在名为 txtSearch 的文本框中输入字符时,无法显示图片 picXmark 并隐藏图片 picEnlarger。

When I tried coding in C# to gain this functionality no effect would occur in the run time.当我尝试在 C# 中编码以获得此功能时,在运行时不会发生任何影响。

I tried using the code:我尝试使用代码:

picEnlarger = new Image();  
picXmark = new Image();

But no effect has happened.但没有发生任何影响。


XAML code from Stock.xaml: XAML 代码来自 Stock.xaml:

<Canvas Height="39.667" Margin="8,0,215.397,0" VerticalAlignment="Top">
    <Button x:Name="btnNewProduct" Content="New" Width="75" Click="btnNewProduct_Click" Height="20.277" RenderTransformOrigin="0.667,1.726" d:LayoutOverrides="VerticalAlignment, Margin" Canvas.Left="0.001" Canvas.Top="18.723" />
    <Button x:Name="btnAddDelivery" Content="Add quantity" Width="75" Click="btnAddDelivery_Click" d:LayoutOverrides="VerticalAlignment, Margin" Height="20.277" Canvas.Left="79.001" Canvas.Top="18.723" />
    <Button x:Name="btnDeleteProduct" Content="Delete" Width="75" RenderTransformOrigin="0.107,1.843" Click="btnDeleteProduct_Click" Height="20.277" Canvas.Left="158.001" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment, Width" Canvas.Top="18.723" />
    <Button x:Name="btnEdit" Content="Edit" Canvas.Left="237.001" Width="75" Canvas.Top="18.723" Click="btnEdit_Click" />
    <TextBox Name="txtSearch" Canvas.Left="391.36" TextWrapping="Wrap" Canvas.Top="18.723" Width="143.243" TextChanged="txtSearch_TextChanged" Text=" Search article" PreviewMouseLeftButtonDown="txtSearch_PreviewMouseLeftButtonDown" TextInput="txtSearch_TextInput">            
        </TextBox>

        <Label Content="Advanced Search" HorizontalAlignment="Left" Canvas.Left="444.289"/>
        <Image x:Name="picXmark" Height="8" Source="/MediaStore;component/Bilder/search_xmark.gif" Stretch="Fill" Width="8" Canvas.Left="519.853" Canvas.Top="24.167" Visibility="Hidden" />
    <Image x:Name="picEnlarger" Height="14" Canvas.Left="513.75" Source="/MediaStore;component/Bilder/search_enlarger2.gif" Stretch="Fill" Canvas.Top="21.527" Width="14" Visibility="Hidden" ImageFailed="picEnlarger_ImageFailed" />

</Canvas>

Class Stock Class 库存

private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{

    picEnlarger = new Image();
    picXmark = new Image();

    if (txtSearch.Text != "")
    {


        picEnlarger.Visibility = Visibility.Collapsed;
        picXmark.Visibility = Visibility.Visible;



        RegularSearch myRegularSearch = new RegularSearch();

        myRegularSearch.Test(txtSearch.Text);

    }
    else
    {
        picEnlarger.Visibility = Visibility.Visible;
        picXmark.Visibility = Visibility.Hidden;                
    }

}


    private void txtSearch_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        txtSearch.Text = "";
    }

In theory you should be able to just use triggers for that, eg从理论上讲,您应该能够为此使用触发器,例如

<TextBox Name="txtSearch" />
<Image Name="ImageOne">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Visibility" Value="Visible" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=txtSearch}"
                             Value="">
                    <Setter Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>
<Image Name="ImageOne">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Visibility" Value="Hidden" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=txtSearch}"
                             Value="">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

When text is entered one image will become visible while the other one will be hidden.输入文本后,一张图像将变为可见,而另一张图像将被隐藏。

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

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