简体   繁体   English

尝试获取IsEnabled绑定到依赖项属性

[英]Trying to get IsEnabled to bind to Dependency Property

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. 我希望在选中与之关联的RadioButton时启用TextBox,然后在选择其他单选按钮时希望将其启用= 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. 我创建了ProxyMode依赖项属性,并根据是否选择了Proxy更改了getter以获取其布尔值。 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 还有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>

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. 根据是否选中了代理RadioButton启用/禁用文本框的最简单方法是将TextBox的IsEnabled属性直接绑定到代理RadioButton的IsChecked属性。 Assuming the proxy RadioButton is named "proxy": 假设代理RadioButton被命名为“ 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). 如果要链接RadioButton控件以便一次只能选择一个,则需要将GroupName属性设置为两个控件上的某个属性(所有链接的RadioButton控件都应该相同)。

Let me know if you have further questions. 如果您还有其他问题,请告诉我。

As with the second version of this question: 与该问题的第二版一样:

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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM