Autor Beitrag
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Fr 17.08.12 16:44 
Hallo Leute!

Auch wenn das Wetter so gut ist- ich habe mal wieder ein Proplem mit WPF, ich denke es dürfte wie so oft eine Keinigkeit sein. Und zwar möchte zwei RadioButtons an ein Boolean binden, dazu habe ich mir diese Tutorial rausgesucht: wpftutorial.net/RadioButton.html und die Aufgabe nachgestellt. Leider bekomme ich eine Exception, das mein Converter nicht verfügbar ist.

Ist meine deklaration für den NameSpace des Converters richtig? :gruebel:

ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
<Window x:Class="WPFRadioButtonBindingtest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:WPFRadioButtonBindingtest"     <-------ist das so korrekt?
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <RadioButton GroupName="group1" IsChecked="{Binding Path=IsTrue,Converter={StaticResource myConverter}, ConverterParameter=option1}" Content="yes" Height="16" HorizontalAlignment="Left" Margin="62,102,0,0" Name="radioButton1" VerticalAlignment="Top" />
        <RadioButton GroupName="group1" IsChecked="{Binding Path=IsTrue, Converter={StaticResource myConverter}, ConverterParameter=option2}" Content="no" Height="16" HorizontalAlignment="Left" Margin="62,132,0,0" Name="radioButton2" VerticalAlignment="Top" />
    </Grid>
    <Window.Resources>
        <c:EnumMatchToBooleanConverter x:Key="myConverter"></c:EnumMatchToBooleanConverter>
    </Window.Resources>
</Window>



Hier mein Code Behind:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
namespace WPFRadioButtonBindingtest
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MyProperties MyProp = new MyProperties();

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = MyProp;
        }
    }

    public class EnumMatchToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return false;

            string checkValue = value.ToString();
            string targetValue = parameter.ToString();
            return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return null;

            bool useValue = (bool)value;
            string targetValue = parameter.ToString();
            if (useValue)
                return Enum.Parse(targetType, targetValue);

            return null;
        }
    }


    public class MyProperties : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool _IsTrue = true;
        public bool IsTrue
        {
            get { return _IsTrue; }
            set
            {
                _IsTrue = value;
            }
        }

        private void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
        }
    }
}




Was habe ich hier falsch gemacht? Über Anregungen und Tipps freue ich mich wie immer!

_________________
Gruß
Christoph
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 17.08.12 17:21 
Bei StaticResource könnte es sein, dass die XAML-Reihenfolge eine Rolle spielt. Setze mal die Ressourcen vor das Grid.

Ganz schlau werde ich aus deinem Converter aber nicht. Du vergleichst einen Boolean IsTrue mit den zwei Strings "option1/2" :gruebel: ?

_________________
>λ=
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Fr 17.08.12 18:23 
Vielen Dank für den Tipp! Das erste Problem lag schon mal an der Reihenfolge. Das war mir bis heute nicht klar, ich habe bisher die Resourcen grundsätzlich am Ende angesiedelt.

Jetzt bekomme ich bei dem Converter eine Exception das der Typ eine Enumeration sein muss. Vielleicht versuche ich noch mal einen anderen Converter, ich verstehe nämlich nicht wirklich was da gemacht wird/werden soll und ich möchte keinen Code verbauen den ich nicht verstehe.

_________________
Gruß
Christoph
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Fr 17.08.12 22:13 
Schade, das oben genante Beispiel konnte ich nicht umsetzten, da nicht alle notwendigen Angaben gemacht wurden. Ich bin die ganze Zeit davon ausgegangen, das an ein Boolean gebunden wird, das ist jedoch nicht der Fall, das Property muss eine Enumeration sein.

Ich habe bei der weiteren Suche dieses Beispiel gefunden:

blog.larshildebrandt...t-page-1#comment-323

Ein sehr gutes Tutorial wie ich finde, mit allen notwendigen Angaben, wenn ich das gleich gefunden hätte, wären mir einige Stunden Kopfzerbrechen erspart geblieben. Na ja, wenigstens habe ich wieder einiges in Sachen WPF dazu gelernt. :nut:

Also, vielen Dank noch mals für die Unterstützung!

_________________
Gruß
Christoph