2009-09-13 80 views
1

我有一個單選按鈕組:數據綁定單選按鈕組屬性

<TextBlock Height="24" Text="Update Interval (min):"/> 
<RadioButton x:Name="radioButtonTimerNone" IsChecked="{Binding UpdateInterval, Converter={StaticResource updateIntervalToCheckedConverter}, Mode=TwoWay}"} Content="None" /> 
<RadioButton x:Name="radioButtonTimerOne" IsChecked="{Binding UpdateInterval, Converter={StaticResource updateIntervalToCheckedConverter}, Mode=TwoWay}" 
       Content="1" /> 
<RadioButton x:Name="radioButtonTimerFive" IsChecked="{Binding UpdateInterval, Converter={StaticResource updateIntervalToCheckedConverter}, Mode=TwoWay}" 
       Content="5" /> 

和屬性:

public int UpdateInterval { 
     get { return _updateInterval; } 
     set { _updateInterval = value; 
      onPropertyChanged("UpdateInterval"); 
     } 
    } 

如何將單選按鈕綁定到屬性,所以radioButtonTimerNone檢查時UpdateInterval爲0 ,當UpdateInterval是1時檢查radioButtonTimerOne等。
我試圖創建一個轉換器,但它不能識別正在設置哪個rb:

[ValueConversion(typeof(RadioButton), typeof(bool))] 
class UpdateIntervalToCheckedConverter : System.Windows.Data.IValueConverter 
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 

我預計'價值'是一個單選按鈕,但它似乎是UpdateInterval的值。

感謝您的任何提示...

回答

1

您的值轉換器不會被告知該單選按鈕改變了價值 - 所有綁定都知道的是,「器isChecked」屬性已更改,所以針對該器isChecked新價值它唯一可以告訴轉換器的東西。

是彈簧我首先想到的是提供與每個綁定的轉換器參數:對「轉換」的方法

<RadioButton 
    x:Name="radioButtonTimerNone" 
    IsChecked="{Binding UpdateInterval, Converter={StaticResource updateIntervalToCheckedConverter}, ConverterParameter=0, Mode=TwoWay}" 
    Content="None" /> 
<RadioButton 
    x:Name="radioButtonTimerOne" 
    IsChecked="{Binding UpdateInterval, Converter={StaticResource updateIntervalToCheckedConverter}, ConverterParameter=1, Mode=TwoWay}" 
    Content="1" /> 
<RadioButton 
    x:Name="radioButtonTimerFive" 
    IsChecked="{Binding UpdateInterval, Converter={StaticResource updateIntervalToCheckedConverter}, ConverterParameter=5, Mode=TwoWay}" 
    Content="5" /> 

所以,現在的「參數」參數將具有值「0 「,」1「或」5「,具體取決於選中哪個RadioButton。我認爲,雖然我不確定,參數將是字符串類型,所以您可能不得不在考慮該值時考慮到這一點。

+0

謝謝,我已經得到它使用您的建議工作。雖然看起來不是很乾淨,但必須通過本質上是我想要綁定到屬性的控制ID。 – Number8 2009-09-13 23:12:15

+0

是的,我不知道是否有一個「更清潔」的解決方案。我並不是一般價值轉換者的忠實粉絲,但是如果你必須使用一個,那麼ConverterParameter可以讓你的生活變得更輕鬆! – 2009-09-13 23:35:51

+0

這是我選擇的解決方案。雖然它不是很乾淨,但我一般都比Anderson在ViewModel上有很多屬性的解決方案更好。但是,我想這是因爲我不太喜歡讓我的ViewModel迎合視圖,因此我可以支持結構化蒙皮。 – dustyburwell 2009-09-14 05:05:39

2

如果你使用MVVM並綁定到ViewModel(我猜你是),我通常認爲我的ViewModel是一個很大的ValueConverter。爲什麼不把這個邏輯放到每個屬性中?

這裏是其中的一個示例:

public bool Timer5Enabled 
{ 
    get { return UpdateInterval == 5; } 
} 

然後你只綁​​定到:

<RadioButton 
    x:Name="radioButtonTimerOne" 
    IsChecked="{Binding Timer5Enabled, Mode=OneWay}" 
    Content="1" /> 

你需要修改的東西將是你的領帶間隔更新邏輯,以提高OnChanged您的相關屬性:

public int UpdateInterval { 
     get { return _updateInterval; } 
     set { _updateInterval = value; 
      onPropertyChanged("UpdateInterval"); 
      onPropertyChanged("Timer5Enabled"); 
      onPropertyChanged("..."); 
     } 
    } 

ValueConverters是很好的避免,如果你能夠。

0

您可以考慮以不同的時間間隔使用List<...>。列表類型應該最好是一些自定義類型(例如UpdateInterval)或KeyValuePair<K,V>以將顯示給用戶的內容與實際值分開。

然後你可以有一個ListBox綁定到這個列表,並且每個ListBoxItem的模板顯示RadioButton。然後,在模板中,將單選按鈕的IsChecked綁定到ListBoxItem.IsSelected

最後一個觸摸只是將您的財產綁定到ListBoxSelectedItemSelectedValue

0

這對我來說是一個非常煩人的問題。

當您將多個RadioButton分配給同一個GroupName時,WPF RadioButton類有一個錯誤消除了IsChecked屬性的綁定。人們已經做了很多工作,但是當我不得不再次訪問它時,我想出了一個不會使我感到噁心的東西。

所以,你必須做的是繼承與此RadioButton類:

public class RadioButton: System.Windows.Controls.RadioButton 
{ 
    protected override void OnClick() 
    { 
     base.OnClick(); 

     SetValue(CurrentValueProperty,CheckedValue); 
    } 

    public int CurrentValue 
    { 
     get { return (int)GetValue(CurrentValueProperty); } 
     set { SetValue(CurrentValueProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for CurrentValue. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty CurrentValueProperty = 
     DependencyProperty.Register("CurrentValue", typeof(int), typeof(RadioButton), new FrameworkPropertyMetadata(0,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, CurrentValue_Changed)); 

    public static void CurrentValue_Changed(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     ((RadioButton)sender).IsChecked = ((RadioButton)sender).CurrentValue == ((RadioButton)sender).CheckedValue; 
    } 

    public int CheckedValue 
    { 
     get { return (int)GetValue(CheckedValueProperty); } 
     set { SetValue(CheckedValueProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for CheckedValue. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty CheckedValueProperty = 
     DependencyProperty.Register("CheckedValue", typeof(int), typeof(RadioButton), new UIPropertyMetadata(0)); 
} 

這樣做只是添加兩個依賴屬性,所以你可以做一個相等比較。如果你發現你要做的就是 1)結合CurrentValue的財產 2)設置的CheckedValue屬性,將使該值

<UserControl x:Class="MyClass" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:c="clr-namespace:MVVMLibrary;assembly=MVVMLibrary" 
    Height="Auto" Width="Auto"> 

      <c:RadioButton CurrentValue="{Binding MyValue}" CheckedValue="1" GroupName="SearchType" >Value 1</c:RadioButton> 
      <c:RadioButton Grid.Column="1" CurrentValue="{Binding MyValue}" CheckedValue="2" GroupName="SearchType" >Value 2</c:RadioButton> 
    </UserControl> 

所以:

所以在XAML的例子是這個RadioButton checked 3)將RadioButtons設置爲相同的GroupName

如果您發現我製作了CurrentValue和CheckedValue int類型。我這樣做的原因是,你可以將CurrentValue綁定到一個枚舉。我認爲這很棒,但這只是我的看法,可能不算太多。 :)

希望這可以幫助別人。

0

一種可能性是實施的IValueConverter並配置它傳遞的預期枚舉值,每單選按鈕,你需要綁定,正如我在我的博客中描述here