试图将属于依赖性属性绑定到依赖性属性
题
任何想法我在这个代码上出错了。我希望在选择关联的radiobutton时启用文本框,然后选择不同的广播按钮时,我希望启用它= false。我创建了一个接头依赖性属性,并根据是否选择了代理,已更改了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>
解决方案
根据是否检查了代理RadioButton,启用/禁用文本框的最简单方法是将文本框的ISENABLED属性直接绑定到代理RadioButton的签名属性。假设代理Radiobutton被称为“代理”:
<TextBox Text="{Binding Path=Proxy}" IsEnabled="{Binding ElementName=proxy, Path=IsChecked}"/>
如果您是要链接的RadioButton控件,以便一次只能选择一个控制,则需要将GroupName属性设置为两者上的某些东西(对于所有链接的RadioButton控件,都应该相同)。
让我知道您是否还有其他问题。
其他提示
与此问题的第二版一样:
<RadioButton x:Name="RadioButton2" />
<TextBox IsEnabled="{Binding IsChecked, ElementName=RadioButton2}" />
认为我想出了一种更好的方法 - 因此,问题可能不是一个好问题 - 没有依赖性属性的以下内容似乎还可以
<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>
不隶属于 StackOverflow