WPF - Togglebutton Sıkıntısı
-
Merhaba hocalar, birkaç gündür bu sorunla uğraşıyorum ve çözebilmiş değilim.
Far kontrol etmek üzere bir arayüzüm var.
View:
http://pastebin.ubuntu.com/11783418/
ViewModel:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media.Animation; using Caliburn.Micro; namespace IKA.ViewModels { public sealed class FarViewModel : Screen { private FarKontrol _farKontrol; public FarViewModel(FarKontrol farKontrol) { _farKontrol = farKontrol; //DisplayName = "Far Kontrol"; } public void RadioButtonHandler(string groupname, string content, bool isChecked) { FarDeger.FarIsim = groupname; FarDeger.Secim = content; FarDeger.isChecked = isChecked; _farKontrol.SendCommand(); } } }
RadioButtonExtension sınıfı, normalde radiobuttonlarla yapıyordum sonradan değiştim ismi öyle kaldı :) Bunun amacı radiobuttonlardaki group özelliğini (yani ikili butonlardan biri açılınca diğeri kapansın mantığı) checkboxlar için uygulamak.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using System.Xml.Linq; using Caliburn.Micro; using FirstFloor.ModernUI.Windows.Media; using MessageBox = System.Windows.Forms.MessageBox; namespace IKA { public class RadioButtonExtension : ToggleButton { // Using a DependencyProperty as the backing store for IsChanged. //This enables animation, styling, binding, etc... public static Dictionary<ToggleButton, String> ElementToGroupNames = new Dictionary<ToggleButton, String>(); public static readonly DependencyProperty GroupNameProperty = DependencyProperty.RegisterAttached("GroupName", typeof(String), typeof(RadioButtonExtension), new PropertyMetadata(String.Empty, OnGroupNameChanged)); public static void SetGroupName(ToggleButton element, String value) { element.SetValue(GroupNameProperty, value); } public static String GetGroupName(ToggleButton element) { return element.GetValue(GroupNameProperty).ToString(); } private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //Add an entry to the group name collection var togglebutton = d as ToggleButton; if (togglebutton != null) { String newGroupName = e.NewValue.ToString(); String oldGroupName = e.OldValue.ToString(); if (String.IsNullOrEmpty(newGroupName)) { //Removing the toggle button from grouping RemoveCheckboxFromGrouping(togglebutton); } else { //Switching to a new group if (newGroupName != oldGroupName) { if (!String.IsNullOrEmpty(oldGroupName)) { //Remove the old group mapping RemoveCheckboxFromGrouping(togglebutton); } ElementToGroupNames.Add(togglebutton, e.NewValue.ToString()); togglebutton.Checked += ToggleButtonChecked; } } } } private static void RemoveCheckboxFromGrouping(ToggleButton checkBox) { ElementToGroupNames.Remove(checkBox); checkBox.Checked -= ToggleButtonChecked; } static void ToggleButtonChecked(object sender, RoutedEventArgs e) { var togglebutton = e.OriginalSource as ToggleButton; foreach (var item in ElementToGroupNames) { if (item.Key != togglebutton && item.Value == GetGroupName(togglebutton)) { item.Key.IsChecked = false; } } } } }
Bir de bu butonların şeklini değiştirdim. Bu İphonelardaki ayar açıp kapama tarzında.Style dosyası, toggle.xaml :
http://pastebin.ubuntu.com/11783416/
Sorun şu ki ben bu togglebuttonu açtığımda da kapadığımda da isChecked değeri hep false olarak dönüyor. Ama stili kaldırınca düzeliyor, neden böyle olduğunu çözebilmiş değilim.
Yardımcı olacaklara şimdiden teşekkürler.
EDIT: XAML kodları doğru düzgün görüntülenemediği için link olarak verilmiştir.
Lightsaber tarafından 27/Haz/15 16:24 tarihinde düzenlenmiştir -
up
-
updulsuavi
-
bir up daha çekelim
-
Kodları incelediğim kadarıyla (wpf anlamam)
item.key.isChecked ' ı birkere kullanmışsın
Koskoca kodda true değeri yok
Tıkladığında vahiyle mi o değer true olacak?
-
by_Tet bunu yazdı
Kodları incelediğim kadarıyla (wpf anlamam)
item.key.isChecked ' ı birkere kullanmışsın
Koskoca kodda true değeri yok
Tıkladığında vahiyle mi o değer true olacak?
zaten sen bastığında o kendi değer alıyor öyle olmasa template'i kaldırınca da true olmazdı false kalırdı hep
-
hala çözebilmiş değilim, bir up daha
-
belki de cozmemen daha hayirli :)
-
unbalanced bunu yazdı
belki de cozmemen daha hayirli :)
çözdüm bile :)
farview.xaml içindeki butonlardan template'i kaldırdım, farview.xaml deki controltemplate i de kaldırdım direk toggle.xaml'deki stili ekledim sorun düzeldi. Nasıl çözemedim ama çalıştığı için ellemeye gerek yok :)