2017-10-14 16 views
0

我想要做的是将两个控件放在相同的位置/点,具体来说是一个按钮,其中包含一个ProgressBar和一个包含图标的按钮,一旦进度栏已满,我想将它切换到具有图标的按钮,如复选框/切换按钮,它有多个状态,当我更改它时,它会更改控件。控件WPF切换2控件在同一地点

形象的例子:

Image

OBS:这种控制的使用MahApps
标题栏控件 OBS2:我使用的材料设计XAML和Caliburn.Micro为真棒MVVM

XAML示例:

<Button FontSize="15" 
       FontWeight="Bold" 
       cal:Message.Attach="UpdateStatus" 
       ToolTip="Click here to show the update status." 
       Visibility="Visible"> 

      <ProgressBar Style="{StaticResource MaterialDesignCircularProgressBar}"      
         Value="0" 
         IsIndeterminate="True" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Center" 
         Height="22" 
         Width="22" Foreground="White"/> 
     </Button> 
     <Button FontSize="15" 
       FontWeight="Bold" 
       cal:Message.Attach="Restart" 
       ToolTip="A update is scheduled, click here to restart the loader and apply the update." 
       Visibility="{Binding Path=RestartButtonVisibility, Mode=TwoWay}"> 

      <materialDesign:PackIcon Height="22" 
            Width="22" 
            Kind="Cached" 
            FontWeight="Bold"/> 
     </Button> 

是否有可能一个完成那个?有没有一种不是意大利面的完美方式?

回答

0

要在同一地点拥有两个UI元素,您可以将它们放在Grid之内。您只需将它们设置为同一行/列(或者根本不设置任何内容)。然后,你可能需要实现一个新的IValueConverter这个技巧。

首先,在您的本地命名空间中的类的地方:

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Data; 

. 
. 
. 

public class MyValueConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     double v = (double)value; 
     if (v == 100) return Visibility.Visible; 
     return Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     //Not really necesary... 
     return 0.0; 
    } 
} 

这是一个值转换器。它与属性绑定一起使用,就像你想要做的一样。

而现在,你的XAML:

<!-- This should be atop... --> 
<Window.Resources> 
    <local:MyValueConverter x:Key="ValueConverter1"/> 
</Window.Resources> 

. 
. 
. 

<Grid> 
    <Button x:Name="Button1"> 
     <ProgressBar x:Name="ProgressBar1" Value="50" Width="100" Height="8"/> 
    </Button> 
    <Button x:Name="Button2" Visibility="{Binding Value, ElementName=ProgressBar1, Converter={StaticResource ValueConverter1}}"> 
     <StackPanel Orientation="Horizontal"> 
      <Image Source="yourIcon"/> 
      <TextBlock Text="Label for your button"/> 
     </StackPanel> 
    </Button> 
</Grid> 

我们使用值转换器到第二个按钮的可见性绑定,这样,如果进度条值是100,给予Visibility属性的值将是Visibility.Visible,反之亦然。

+0

更好地写'返回(双)值<100? Visibility.Collapsed:Visibility.Visible',或'return(double)value <100? Visibility.Hidden:Visibility.Visible',如果你想提前完成布局。如果转换器不支持反向转换,则抛出NotSupportedException。只是要有点细致。 – Clemens

+0

谢谢你们,网格技巧解决了这个问题,但我会以另一种方式处理可见性变化。 –

0

您可以将第一个按钮的可见性与第二个VM中的属性绑定,并使它们互斥(一个可见,然后其他已折叠)。在进度条完成结束时触发onpropertychanged,你应该很好。