将图片显示为隐藏或可见

时间:2011-06-04 19:46:14

标签: c# wpf image xaml visibility

目标:
当用户开始在文本框中键入文本或字符时txtSearch图片picEnlarger将被隐藏并被图片picXmark替换。 默认情况下,picEnlarger将始终显示,直到输入数据将应用于文本框txtSearch。在顺序字中,文本框中没有数据然后显示picEnlarger并隐藏picXmark。

问题:
当用户开始在名为txtSearch的文本框中键入字符时,显示图片picXmark并隐藏图片picEnlarger有问题。

当我尝试使用C#进行编码以获得此功能时,在运行时不会产生任何影响。

我尝试使用代码:

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

但没有发生任何影响。


Stock.xaml的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>

类库存

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 = "";
    }

1 个答案:

答案 0 :(得分:1)

理论上你应该只能使用触发器,例如

<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>

输入文字时,一个图像将变为可见,而另一个图像将被隐藏。