【问题标题】:Trying to get IsEnabled to bind to Dependency Property试图让 IsEnabled 绑定到依赖属性
【发布时间】:2010-09-10 01:38:13
【问题描述】:

知道这段代码哪里出了问题。我希望在选择关联的radiobutton时启用文本框,然后选择不同的广播按钮时,我希望启用它= false。我创建了一个 ProxyMode 依赖属性,并根据是否选择了 Proxy 更改了 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));

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

【问题讨论】:

    标签: c# .net wpf binding isenabled


    【解决方案1】:

    根据是否选中代理 RadioButton 来启用/禁用文本框的最简单方法是将 TextBox 的 IsEnabled 属性直接绑定到代理 RadioButton 的 IsChecked 属性。假设代理 RadioButton 被命名为“proxy”:

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

    如果您打算链接 RadioButton 控件以便一次只能选择一个,则需要将 GroupName 属性设置为它们上的某个值(对于所有链接的 RadioButton 控件,它应该相同)。

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

    【讨论】:

      【解决方案2】:

      与这个问题的第二个版本一样:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 你在我回答后几秒钟就发布了,但是是的,基本上和我回答的一样。
        猜你喜欢
        • 2016-01-22
        • 2023-03-26
        • 2018-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        相关资源
        最近更新 更多