我的usercontrol上有两个组合框。我的目标是,当选择第一个(内有项目)时,第二个应该是活动的。 以下是代码段:
<UserControl x:Class="RestoreComputer.Views.ConfigView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="20,0,0,0" Height="50">
<ComboBox Name="_server" ItemsSource="{Binding Path=Servers}" SelectedItem="{Binding Server}" IsSynchronizedWithCurrentItem="True" Width="100" VerticalContentAlignment="Center" Text="18"/>
<Image Source="../Images/narrow.png" Margin="10,0,10,0"/>
<ComboBox Name="_computer" IsEnabled="False"/>
</StackPanel>
</Grid>
</UserControl>
如果选择第一个项目,如何激活第二个组合框?
答案 0 :(得分:3)
由于您已经在使用数据绑定,因此可以在viewmodel中创建一个布尔属性,并在设置Server属性时将其设置为true。并将该属性绑定到您的第二个组合框启用属性。
在您的viewmodel
中private bool comboEnabled = false;
public bool ComboEnabled
{
get
{
return comboEnabled;
}
set
{
comboEnabled = value;
onPropertyChanged("ComboEnabled");
}
}
//and in your Server property
public Server
{
get---YourCode{}
set
{
if(value != null)
ComboEnabled = true;
---Yourcode
}
}
//and in your xaml
<ComboBox Name="_computer" IsEnabled="{Binding ComboEnabled }"/>
答案 1 :(得分:0)
有效 - 你的意思是“启用”?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
{
comboBox2.Enabled = true;
}
else
{
comboBox2.Enabled = false;
}
}
甚至更简单
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Enabled = comboBox1.SelectedIndex != -1;
}
答案 2 :(得分:0)
在第一个组合框上创建一个SelectionChanged事件: (这可以在wpf或Load事件中完成)
_server.SelectionChanged += OnSelectionChanged;
获取方法
private void OnSelectionChanged(object o, eventArgs e)
{
//Do whatever you want to do with the selection data
DoSomething();
//Focus the second combobox:
_computer.Focus();
}