mayho33
Goto Top

MVVM Button.IsEnabled binding funktioniert nicht

Hallo @ All

Ich hatte schon mal eine ähnliche Frage, bei dieser hier schaut es aber etwas anders. Hoffe trotzdem auf eure Hilfe um mir etwas auf die Sprünge zu helfen.

Aufbau:

Ich habe ein MainWindow mit einem Button der über ein StyleTemplate auf True gesetzt ist. EinTrigger sollte es entsprechend dem Property EnableBuildButton auf False setzen (<= passiert nicht). Abgeschaut von hier: http://stackoverflow.com/questions/31967085/binding-button-isenabled-to ...

<Style x:Key="BuildButton" TargetType="{x:Type Button}">  
        <Setter Property="Foreground" Value="White" />  
        <Setter Property="IsEnabled" Value="True" />  
        <Setter Property="Template">  
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">  
                    <Border
                        Background="{TemplateBinding Background}"  
                        BorderBrush="{TemplateBinding BorderBrush}"  
                        BorderThickness="2"  
                        CornerRadius="20">  
                        <ContentPresenter
                            HorizontalAlignment="Center"  
                            VerticalAlignment="Center"  
                            TextElement.FontWeight="Bold" />  
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=EnableBuildButton}" Value="False">  
                <Setter Property="IsEnabled" Value="False"/>  
            </DataTrigger>
            <Trigger Property="IsMouseOver" Value="False">  
                <Setter Property="BorderBrush" Value="#428BCA" />  
                <Setter Property="Background" Value="#006CCA" />  
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">  
                <Setter Property="BorderBrush" Value="#69C9F4" />  
                <Setter Property="Background" Value="#00A8F4" />  
            </Trigger>
            <Trigger Property="IsPressed" Value="True">  
                <Setter Property="Background" Value="#75DE52" />  
                <Setter Property="BorderBrush" Value="#37DE00" />  
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">  
                <Setter Property="Background" Value="#878787" />  
                <Setter Property="BorderBrush" Value="#595959" />  
            </Trigger>
        </Style.Triggers>
    </Style>


3 weitere Properties, welche wiederum aus einer anderen Class (siehe weiter unten) aktualisiert werden, aktualisieren das Property EnableBuildButton mit True. Das Property wird nur aktualisiert, wenn alle 3 anderen Properties !string.IsNullOrEmpty() sind
Namespace myTest
{
public partial class MainWindow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private static bool _EnableBuildButton = false;
        public bool EnableBuildButton
        {
            get { _EnableBuildButton; }
            set
            {
                if (!string.IsNullOrEmpty(AssetID) && !string.IsNullOrEmpty(ApplicationName) && !string.IsNullOrEmpty(ApplicationVersion))
                    _EnableBuildButton = true;
                else
                    return;
                RaisePropertyChanged("EnableBuildButton");  
            }
        }
}

private static string _AssetID;
        public static string AssetID { 
            get { return _AssetID; }
            set
            {
                _AssetID = value;
                new MainWindow().EnableBuildButton = true;
            }
        }
        private static string _ApplicationName;
        public static string ApplicationName
        {
            get { return _ApplicationName; }
            set
            {
                _ApplicationName = value;
                new MainWindow().EnableBuildButton = true;
            }
        }
        private static string _ApplicationVersion;
        public static string ApplicationVersion
        {
            get { return _ApplicationVersion; }
            set
            {
                _ApplicationVersion = value;
                new MainWindow().EnableBuildButton = true;
            }
        }

in einem, im MainWindow eingebetteten UserControl habe ich 3 TextBoxes die ebenfalls gebinded sind und gleichzeitig, wie oben erwähnt, die Properties in MainWindow aktualisieren.
namespace myTest
{
    public partial class WizardBuild : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private static string _AssetID;
        public string AssetID
        {
            get { return _AssetID; }
            set
            {
                if (_AssetID == value)
                    return;

                _AssetID = value;
                NotifyPropertyChanged("AssetID");  
                MainWindow.AssetID = value;
            }
        }

        private static string _ApplicationName;
        public string ApplicationName
        {
            get { return _ApplicationName; }
            set
            {
                if (_ApplicationName == value)
                    return;

                _ApplicationName = value;
                NotifyPropertyChanged("ApplicationName");  
                MainWindow.ApplicationName = value;
            }
        }

        private static string _ApplicationVersion;
        public string ApplicationVersion
        {
            get { return _ApplicationVersion; }
            set
            {
                if (_ApplicationVersion == value)
                    return;

                _ApplicationVersion = value;
                NotifyPropertyChanged("ApplicationVersion");  
                MainWindow.ApplicationVersion = value;
            }
        }
    }
}

Das Problem kurz und bündig:

Alle Properties der textBoxes haben text, die Prüfung in "EnableBuildButton" ist positiv, das Property wird aktualisiert, aber
RaisePropertyChanged("EnableBuildButton"); ist immer null. Ich vermute einen Verlust des DataContext, aber dann dürfte das Property ja nicht aktualisiert werden? Was aber der Fall ist. Hat jemand eine Idee wo der Wurm drinnen ist? Ich stehe am Schlauch.

Vielen Dank für die Unterstützung!

Grüße!

Mayho

Content-Key: 330006

Url: https://administrator.de/contentid/330006

Ausgedruckt am: 29.03.2024 um 06:03 Uhr