Silverlight 如何让2个combox彼此交互
我同时附加了XAML和C#代码,但它不起作用。请告知。谢谢Silverlight 如何让2个combox彼此交互,silverlight,Silverlight,我同时附加了XAML和C#代码,但它不起作用。请告知。谢谢 在此处输入代码 <Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefi
在此处输入代码
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.7*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="0.7*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="5*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="Select Country" Grid.Row="0" Grid.Column="0"></TextBlock>
<ComboBox x:Name="CmbxCountry" SelectionChanged="CmbxCountry_SelectionChanged" Grid.Row="0" Grid.Column="1"></ComboBox>
<TextBlock Text="Select State" Grid.Row="2" Grid.Column="0"></TextBlock>
<ComboBox x:Name="CmbxState" Grid.Row="2" Grid.Column="1"></ComboBox>
</Grid>
名称空间模型
{
公共部分类主页面:UserControl
{
//私有画笔_selectedBrush=新的SolidColorBrush(Color.FromArgb(255,255,0,0));
//私有画笔_normalBrush=新的SolidColorBrush(Color.FromArgb(255,0,0,0))
public主页()
{
初始化组件();
this.Loaded+=新的RoutedEventHandler(主页面_-Loaded);
}
已加载无效主页(对象发送器、路由目标)
{
列表=新列表
{
新国家{Name=“USA”},
新国家{Name=“China”},
新国家{States=GetStates()}
};
CmbxCountry.ItemsSource=列表;
CmbxCountry.DisplayMemberPath=“名称”;
}
私有列表GetStates()
{
列表=新列表
{
新州{Name=“NJ”},
新州{Name=“NY”},
新状态{Name=“NV”},
新状态{Name=“CT”},
新状态{Name=“CA”}
};
退货清单;
}
私有无效CmbxCountry\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
var cmbx=发送方作为组合框;
var selectedItem=cmbx.selectedItem作为国家/地区;
CmbxState.ItemsSource=selectedItem.State;
CmbxState.DisplayMemberPath=“名称”;
}
}
公营国家
{
公共字符串名称{get;set;}
公共列表状态{get;set;}
}
公共阶级国家
{
公共字符串名称{get;set;}
}
}对我有用。尽管我认为您在第一个组合框中设置了错误的项。目前,您的“国家”组合框中填充了以下国家:“美国”、“中国”、“美国”。无论何时选择第三个(“”),都会得到非空状态列表 如果您希望在当前实施中每个国家都有州,我建议如下:
List<Country> list = new List<Country>
{
new Country{ Name = "USA", States = GetStates() },
new Country{ Name = "China", States = new List<State>() },
};
List List=新列表
{
新国家{Name=“USA”,States=GetStates()},
新国家{Name=“China”,States=new List()},
};
此外,我建议将其重构为具有绑定的MVVM,使用包含选定国家/地区的模型,并相应地更新第二个组合框值:
public class MainViewModel : INotifyPropertyChanged
{
private Country _selectedCountry;
public ObservableCollection<Country> Countries { get; }
public Country SelectedCountry
{
get { return _selectedCountry; }
set { _selectedCountry = value; RaisePropertyChanged("SelectedCountry"); RaisePropertyChanged("States"); }
}
public State[] States
{
get
{
if (_selectedCountry == null)
return new State[0];
return _selectedCountry.States.ToArray();
}
}
}
public类主视图模型:INotifyPropertyChanged
{
私人国家(u选择的国家);;
公共可观测收集国家{get;}
公共国家/地区选择国家/地区
{
获取{return\u selectedCountry;}
设置{u selectedCountry=value;RaisePropertyChanged(“selectedCountry”);RaisePropertyChanged(“States”);}
}
公共国家[]国家
{
得到
{
如果(_selectedCountry==null)
返回新状态[0];
返回_selectedCountry.States.ToArray();
}
}
}
而且您的视图(xaml)不必定义事件,只需使用绑定
public class MainViewModel : INotifyPropertyChanged
{
private Country _selectedCountry;
public ObservableCollection<Country> Countries { get; }
public Country SelectedCountry
{
get { return _selectedCountry; }
set { _selectedCountry = value; RaisePropertyChanged("SelectedCountry"); RaisePropertyChanged("States"); }
}
public State[] States
{
get
{
if (_selectedCountry == null)
return new State[0];
return _selectedCountry.States.ToArray();
}
}
}