2010-09-10 66 views
1

任何想法,我錯了這個代碼。我希望文本框被啓用,當它的關聯RadioButton被選中,然後當一個不同的單選按鈕被選中時,我希望它是Enabled = False。我創建了一個ProxyMode依賴項屬性,並根據是否選擇了代理來更改getter以獲取它的bool值。似乎沒有工作......任何想法?試圖讓IsEnabled綁定到依賴項屬性

// Proxy Host Name 
public string Proxy 
{ 
    get { return (string)GetValue(ProxyProperty); } 
    set { SetValue(ProxyProperty, value); } 
} 

public static readonly DependencyProperty ProxyProperty = 
     DependencyProperty.Register("Proxy", typeof(string), typeof(ConfigWindowViewModel), new UIPropertyMetadata("[e.g. proxy.mycompany.com]")); 

public bool ProxyMode 
{ 
    get { return Proxy == "Proxy"; } 
    set { SetValue(ProxyModeProperty, value); } 
} 

public static readonly DependencyProperty ProxyModeProperty = 
     DependencyProperty.Register("ProxyMode", typeof(bool), typeof(ConfigWindowViewModel)); 

而XAML

<StackPanel Grid.Column="0" Margin="2"> 
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
    <RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Proxy}" 
        VerticalAlignment="Center" 
        Padding="2,0,10,0">Proxy 
    </RadioButton> 
    <TextBox Text="{Binding Path=Proxy}" 
      IsEnabled="{Binding Path=ProxyMode}" 
      Width="Auto" 
      Name="ProxyHostTextBox" 
      VerticalAlignment="Center" 
      MinWidth="150" 
    /> 
    </StackPanel> 
    <RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Direct}">Direct</RadioButton> 
</StackPanel> 

回答

4

啓用/禁用基於單選按鈕被選中的代理是否TextBox的IsEnabled屬性直接綁定到的該器isChecked屬性的文本框的最簡單方法代理RadioButton。假設代理單選按鈕被命名爲「代理」:

<TextBox Text="{Binding Path=Proxy}" IsEnabled="{Binding ElementName=proxy, Path=IsChecked}"/> 

如果你的意思是你的單選按鈕控制要鏈接,這樣,只有一個可以一次選擇,你需要設置GroupName屬性的東西在他們兩人(它應該與所有鏈接的RadioButton控件相同)。

讓我知道你是否還有其他問題。

0

覺得我想出了一個更好的辦法來做到這一點 - 所以這個問題可能不是一個好問 - 不依賴屬性下面似乎確定

 <StackPanel Grid.Column="0" Margin="2"> 
      <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
       <RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Proxy}" VerticalAlignment="Center" Padding="2,0,10,0" Name="ProxyModeRadioButton">Proxy</RadioButton> 
       <TextBox Text="{Binding Path=Proxy}" 
         IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked}" 
         Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" 
       /> 

      </StackPanel> 
      <RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Direct}">Direct</RadioButton> 
     </StackPanel> 
+0

你在我的回答後張貼了這幾秒,但是,基本上和我回答的一樣。 – Kelsie 2010-09-10 02:03:28

2

與這個第二個版本問題:

<RadioButton x:Name="RadioButton2" /> 
<TextBox IsEnabled="{Binding IsChecked, ElementName=RadioButton2}" />