尝试让 IsEnabled 绑定到依赖属性

发布于 2024-09-18 11:23:36 字数 1615 浏览 7 评论 0原文

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

Any idea where I'm going wrong with this code. I want the TextBox to be Enabled when the associated RadioButton for it is selected, and then when a different radio button is selected I want it to be Enabled=False. I created a ProxyMode dependency property and have changed the getter to obtain it's bool value based on whether Proxy is selected or not. Doesn't seem to work...any ideas?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风尘浪孓 2024-09-25 11:23:36

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

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

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

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

The easiest way to enable/disable the textbox based on whether the proxy RadioButton is checked is to bind the IsEnabled property of the TextBox directly to the IsChecked property of the proxy RadioButton. Assuming the proxy RadioButton is named "proxy":

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

If you meant for your RadioButton controls to be linked so that only one can be selected at once, you need to set the GroupName property to something on both of them (it should be the same for all linked RadioButton controls).

Let me know if you have further questions.

仙女 2024-09-25 11:23:36

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

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

As with the second version of this question:

<RadioButton x:Name="RadioButton2" />
<TextBox IsEnabled="{Binding IsChecked, ElementName=RadioButton2}" />
只有一腔孤勇 2024-09-25 11:23:36

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

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

think I came up with a better way to do this - so the question probably isn't a good one to ask - the following without dependency property seem ok

        <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文