我有一个xaml表单(相关代码如下)。我希望在用户点击搜索按钮时显示文本/标签或处理图像,并且应该在结果返回时隐藏。
PS:我不是一名xaml程序员。我知道asp.net mvc(c#),但得到了这个快速的任务。
以下是要显示的按钮和标签的代码:
<Button Grid.Row="4" Content="{Binding SEARCH}" Grid.Column="8" Foreground="White" Command="{Binding ResubmitQuote}" Height="25" x:Name="SearchButton" Click="SearchButton_Click" >
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#753297"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#9966cc"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
标签代码(可以用文字或图片代替):
<Label Grid.Row="2" Grid.Column="8" Foreground="#6B666E" FontSize="10" Padding="20, 8, 0, 0" Content="{Binding SEARCH}" x:Name="searchingLabel" Opacity="0" />
我已经尝试过这个
private void SearchButton_Click(object sender, RoutedEventArgs e) {
this.searchingLabel.Opacity = 1;
}
但是这会显示结果返回时的标签。
答案 0 :(得分:1)
在ViewModel中创建一个属性
private Visibility fieldtest;
public Visibility MyPropertytest
{
get
{
return this.fieldtest;
}
set
{
this.fieldtest = value;
this.OnPropertyChanged("MyPropertytest");
}
}
private void ResubmitQuote(object obj)
{
Dispatcher.Invoke(MyPropertytest = Visibility.Visible, System.Windows.Threading.DispatcherPriority.Normal);
////Load your data Here for DataBase
Dispatcher.Invoke(MyPropertytest = Visibility.Collapsed, System.Windows.Threading.DispatcherPriority.Normal)
}
将此属性与您的标签绑定
Visibility="{Binding MyPropertytest}"
答案 1 :(得分:1)
在按钮命令中使用此代码它对我来说很好
XAML
<Label Foreground="#6B666E" FontSize="10" Content="Loading" x:Name="searchingLabel" Visibility="{Binding LabelVisibility}" />
视图模型
private void ExecuteCommand(object obj)
{
LabelVisibility = Visibility.Visible;
Application.Current.Dispatcher.Invoke((Action)(() => { }), DispatcherPriority.Render);
//// Do your code here
LabelVisibility = Visibility.Collapsed;
}
答案 2 :(得分:1)
我建议您考虑使用模型视图模型模式来管理App的导航逻辑和内容。 (Channel9 introductive video)
作为MVVM框架,您可以使用MVVM Light library公开一些有用的方法。
在这种情况下,我会实施RelayCommand
来管理按钮的操作,并IValueConverter
来管理内容的可见性。
按钮的点击行为:
private RelayCommand _searchCommand;
public RelayCommand SearchCommand
{
get
{
return _searchCommand
?? (_searchCommand = new RelayCommand(
async () =>
{
IsProcessing = true;
// Your Logic, for example
// await DoSomething
IsProcessing = false;
}
}
}
按钮的XAML :
<Button ... Command={Binding SearchCommand} ... />
您的可见性转换器:
public class TrueCollapsedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (bool)value ? Visibility.Collapsed: Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return null;
}
}
您的属性让您的View知道您何时处理某些内容(定义在您的ViewModel类中):
public bool IsProcessing
{
get
{
return _isProcessing;
}
set
{
if (_isProcessing == value)
{
return;
}
_isProcessing = value;
RaisePropertyChanged(() => IsProcessing);
}
}
您的XAML 定义转换器(您可以将其放入App.xaml或Page.Resources标记中):
<converters:TrueCollapsedConverter x:Key="TrueCollapsedConverter" />
您想要管理可见性的对象/对象的XAML :
<Label ... Visibility={Binding IsProcessing, Converter={StaticResource TrueCollapsedConverter} ... />