UserControl - 声明自定义枚举,然后在VM中使用它

时间:2017-07-14 07:22:50

标签: c# wpf mvvm

我有自定义控件:

<UserControl>
<Grid x:Name="Layout">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Content="{Binding ProtocolType, TargetNullValue='https://'}" />
    <TextBox Grid.Column="1"
             VerticalContentAlignment="Center" MinWidth="200" Text="{Binding Hostname, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
    <Label Grid.Column="2" Content="{Binding PathType}" />
    <ProgressBar Grid.Column="3" IsIndeterminate="True" Width="40" Height="10"
                 HorizontalContentAlignment="Center" />
    <Image Grid.Column="3" Width="16" Height="16" />
</Grid>

如您所见,它有进度条和图像。进度条表示,当某事正在进行时,Image表示结果 - 好/坏。我想要的是,当我按下MainView上的按钮时,将显示进度条,当操作完成时,结果将显示为图像并隐藏进度条。 我的计划是用4个州宣布枚举 - 没有,进展,好,坏。但是我不确定,当我声明枚举并用它来操作usercontrol中的一些控件时,如果我可以在我的ViewModel中使用这个枚举。如果我不违反MVVM。

修改 枚举:

public enum Progress
    {
        InProgress,
        Success,
        Failed,
        Nothing
    }

VM:

private Progress _webServiceProgress;

    public Progress WebServiceProgress
    {
        get { return _webServiceProgress; }
        set { Set(ref _webServiceProgress, value); }
    }

此VM的属性我将绑定到我的usercontrol属性(DP),根据枚举值,我将显示progressbar / image。

1 个答案:

答案 0 :(得分:0)

您可以使用触发器定义ControlTemplate来设置控件的Visibility属性:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <Grid x:Name="Layout">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" Content="{Binding ProtocolType, TargetNullValue='https://'}" />
                <TextBox Grid.Column="1"
             VerticalContentAlignment="Center" MinWidth="200" Text="{Binding Hostname, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
                <Label Grid.Column="2" Content="{Binding PathType}" />
                <ProgressBar x:Name="pb" Grid.Column="3" IsIndeterminate="True" Width="40" Height="10"
                 HorizontalContentAlignment="Center" />
                <Image x:Name="img" Grid.Column="3" Width="16" Height="16" />
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding WebServiceProgress}" Value="{x:Static local.Progress.InProgress}">
                    <Setter TargetName="img" Property="Visibility" Value="Hidden" />
                    <Setter TargetName="pb" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding WebServiceProgress}" Value="{x:Static local.Progress.Success}">
                    <Setter TargetName="img" Property="Visibility" Value="Visible" />
                    <Setter TargetName="pb" Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>