开发者

How can I show a button since XAML?

Let me provide you more details. The situation is I'm working on a user control and I've got a dependency object where receives a enum. Depending of the value, must show a button or not.

I mean:

public enum 开发者_Python百科Entradas
{
    Entero, Decimal
}

public partial class TableroUserControl : UserControl
{
    public Entradas Entrada
    {
        get { return (Entradas)GetValue(EntradaProperty); }
        set { SetValue(EntradaProperty, value); }
    }

    public static readonly DependencyProperty EntradaProperty =
        DependencyProperty.Register("Entrada", typeof(Entradas), typeof(TableroUserControl));
}

When EntradaProperty receives Entradas.Entero, it must show a button in the user control, and when put Decimal, must disappears the button. Although, the property must contain a default value too.

I don't know if a must declare a PropertyMetadata object in EntradaProperty or use a IValueConverter.

How can I do that? Thanks in advance.


You can create an IValueConverter implementation to do what you need. The result will be a System.Windows.Visibility object;

class EntradasToVisibilityConverter : IValueConverter
{
    public Object Convert(
    Object value,
    Type targetType,
    Object parameter,
    CultureInfo culture )
    {
        // error checking, make sure 'value' is of type
        // Entradas, make sure 'targetType' is of type 'Visibility', etc.

        return (((Entradas)value) == Entradas.Entero) 
                ? Visibility.Visible
                : Visibility.Collapsed;
    }

    public object ConvertBack(
    object value,
    Type targetType,
    object parameter,
    CultureInfo culture )
    {
        // you probably don't need a conversion from Visibility
        // to Entradas, but if you do do it here
        return null;
    }
}

Now, in XAML...

<SomeParentControl.Resources>
    <myxmlns:EntradasToVisibilityConverter x:key="MyEntradasToVisConverter" />
</SomeParentControl.Resources>
<Button
    Visibility="{Binding MyEnumValue, Converter={StaticResource MyEntradasToVisConverter}}"
/>


You can do this either by metadata or by a ValueConverter. THe examples for the valueConverter are already given. Here an example for doing it by metadata.

public static readonly DependencyProperty EntradaProperty = 
        DependencyProperty.Register("Entrada", typeof(Entradas), typeof(TableroUserControl), new UIPropertyMetadata((d,e)=> { ((TableroUserControl)d).EntradaPropertyChanged(e); }));

private EntradaPropertyChanged(DependencyPropertyChangedEventArgs e){
  Entradas entrada=(Entradas)e.NewValue ;
  if(entrada=Entradas.Entero)
     // Show your control 
  }else{
     // Hide your control
  }
}


You can use your custom IValueConverter or declare DataTrigger in TableroUserControl's XAML.


If EntradaProperty is not changed in any other way than through the property Entrada this should work:

public Entradas Entrada
{
    get { return (Entradas)GetValue(EntradaProperty); }
    set 
    { 
        SetValue(EntradaProperty, value); 
        if (Entrada == Entradas.Entero)
            //show button
        else
            //hide button
    }
}

the default value would have to be specified elsewhere, but Entradas will start as Entero sou showing the button at the beginning should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜