gpt4 book ai didi

c# - 将图片显示为隐藏或可见

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:41 43 4
gpt4 key购买 nike

目标:
当用户开始在文本框 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 = "";
}

最佳答案

理论上你应该能够为此使用触发器,例如

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

当输入文本时,一张图片将可见,而另一张将隐藏。

关于c# - 将图片显示为隐藏或可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6238993/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com