简体   繁体   中英

change style of button with enum dependency property. How to properly bind

i am trying to change the style of my button based on its own dependency property. i cant seem to figure out why this is not working. it has got to do with the enum and how to bind it. im am new to WPF and ive been searching forever. please help! the relevant code. first my button class:

public class AmmoType
{
    public enum ammoType { RFS, RFD, RFC, EMPTY }
}

public class DLSButton : Button
{
    public static readonly DependencyProperty AmmoTypeProperty;
    public AmmoType.ammoType ammoTypeEnum
    {
        get
        {
            return (AmmoType.ammoType)GetValue(DLSButton.AmmoTypeProperty);
        }
        set
        {
            SetValue(DLSButton.AmmoTypeProperty, value);
        }
    }
    static DLSButton()
    {
        DLSButton.AmmoTypeProperty = DependencyProperty.Register("ammoTypeEnum", typeof(AmmoType.ammoType), typeof(DLSButton), new FrameworkPropertyMetadata(AmmoType.ammoType.EMPTY));
    }
}

my application Ressources (App.xml):

<Application.Resources>        
    <Style TargetType="{x:Type local:DLSButton}" x:Key="DLSAmmo">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Background" Value="LightGray"/>
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="1,1,1,1" />
        <Setter Property="IsEnabled" Value="False"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Viewbox StretchDirection="Both" >
                        <TextBlock FontWeight="Bold" TextWrapping="Wrap">DLS</TextBlock>
                    </Viewbox>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=AmmoType+ammoType}" Value="{x:Static my:AmmoType+ammoType.EMPTY}">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Red"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">N/A</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ammoTypeEnum}" Value="RFD">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Green"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">RFD</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ammoTypeEnum}" Value="{x:Static local:AmmoType+ammoType.RFS}">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Green"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">RFS</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ammoTypeEnum}" Value="{x:Static local:AmmoType+ammoType.RFC}">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Green"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">RFC</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>                
    </Style>
</Application.Resources>

my XAML where i insert my button:

<local:DLSButton ammoTypeEnum="EMPTY" x:Name="buttonDLS1" Style="{StaticResource DLSAmmo}">                            
                    </local:DLSButton>

no errors are shown in error list. but when i build the solution a messagebox tells me that: "the property value is not valid"

and as you can see in the different 'DataTriggers' i have tried different things with binding. still no errors. and still no style changing...

After some fiddling, I remembered that I hadn't set the data context when I copy/pasta'd your code. That did the trick after fixing some of the datatrigger bindings and values to the values you already had working. Here's what I changed, and it works on my computer:

App.xaml

 // so we can see the bg change, but the text changed without it
<Setter Property="IsEnabled" Value="True"/>
...
// for each of the datatriggers
<DataTrigger Binding="{Binding Path=ammoTypeEnum}" 
             Value="{x:Static local:AmmoType+ammoType.XXX}">

DLSButton.cs

// this should be obvious
public partial class DLSButton : Button
{
    public DLSButton()
    {
        InitializeComponent();
        DataContext = this;
    }
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM