C#-访问.xaml已创建复选框/列表框
在.xaml中创建列表框时,是否有方法验证是否选中了复选框和列表框中的特定项 我试图从不同的类而不是ViewModel访问这些元素 我希望能够做到以下几点:C#-访问.xaml已创建复选框/列表框,c#,checkbox,listbox,generic.xaml,C#,Checkbox,Listbox,Generic.xaml,在.xaml中创建列表框时,是否有方法验证是否选中了复选框和列表框中的特定项 我试图从不同的类而不是ViewModel访问这些元素 我希望能够做到以下几点: if (First_CheckBox.Ischecked && LocationBox.SelectedItem == "FirstValue") { do something; } else { do something else; } .XAML代码: <CheckBox x:Name="First_
if (First_CheckBox.Ischecked && LocationBox.SelectedItem == "FirstValue")
{
do something;
}
else
{
do something else;
}
.XAML代码:
<CheckBox x:Name="First_CheckBox" IsChecked="{Binding Check, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Content="Integral "/>
<ListBox x:Name="LocationBox" ItemsSource="{Binding LocationList}" SelectionMode="Single" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Margin="0,5" Width="100" HorizontalAlignment="Left" BorderThickness="1" Background="#FFF0ECEC">
首先,
First\u复选框。选中的
很可能是First\u复选框。已选中
但您实际遇到了哪些错误?您在哪里访问在xaml中创建的元素,代码隐藏和viewmodel之间有很大的区别?我试图在一个单独的绘制形状的类中访问元素。对ListBox使用GetItemText()方法:string text=LocationBox.GetItemText(LocationBox.SelectedItem);如果该单独的类不是后面的代码,那么您将无法按名称访问元素,例如“First_CheckBox”。您需要访问数据绑定的属性,例如if(Check){//do this}
@dre我同意JSteward的建议。属性是这里封装的最佳实现。
namespace ValueClearanceCalculator.ViewModels
{
class MainWindowViewModel : CommonBase
{
public delegate void PropertyChangedHandler(object obj);
public static event PropertyChangedHandler MainVMPropertyChanged = delegate { };
public MainWindowViewModel()
{
CalculateAndDrawPatternCommand = new CalculatePatternCommand(this);
}
private public bool _Check;
public bool Check
{
get
{
return _Check;
}
set
{
_Check = value;
RaisePropertyChanged("Check");
_C = _A;
double temp;
bool result = double.TryParse(_C, out temp);
if (result == false) { MessageBox.Show("Can Not Convert A to C"); }
else { FrontPanelVariables.C = temp * 25.4; }
RaisePropertyChanged("C");
_F = ".157";
FrontPanelVariables.F = .157;
RaisePropertyChanged("F");
}
}