代码之家  ›  专栏  ›  技术社区  ›  Greg

正在尝试使IsEnabled绑定到依赖项属性

  •  1
  • Greg  · 技术社区  · 14 年前

    知道我的代码哪里出错了吗。我希望文本框在选中关联的单选按钮时启用,然后在选择其他单选按钮时,我希望它的Enabled=False。我创建了一个ProxyMode依赖属性,并根据是否选择了代理来更改getter以获取其bool值。好像不行…有什么想法吗?

    // 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));
    

    <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>
    
    3 回复  |  直到 11 年前
        1
  •  4
  •   Kelsie    14 年前

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

    如果要链接RadioButton控件,以便一次只能选择一个,则需要在两个控件上都设置GroupName属性(对于所有链接的RadioButton控件,该属性应相同)。

    如果你还有其他问题,请告诉我。

        2
  •  2
  •   Jay    14 年前

    关于这个问题的第二个版本:

    <RadioButton x:Name="RadioButton2" />
    <TextBox IsEnabled="{Binding IsChecked, ElementName=RadioButton2}" />
    
        3
  •  0
  •   Greg    14 年前

    我想我想出了一个更好的方法来做到这一点-所以这个问题可能不是一个好的问题问-下面没有依赖属性似乎可以

            <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>