问题描述
您好,
我正在使用MVVMLight开发WP8应用程序。我有4个单选按钮和一个文本框。我希望文本框对于前两个单选按钮应该是可见的,对于其余按钮它应该是不可见的。我该怎么做?
I am develeoping WP8 app using MVVMLight. I have 4 radio buttons and one textbox. I want textbox should be visible for first two radio buttons and for rest of the buttons it should not be visible.How should I do that?
Amol Gandhi
Amol Gandhi
推荐答案
>> 我想要textbox应该对于前两个单选按钮是可见的,对于其余的按钮,它不应该是可见的。我该怎么做?
我想知道你是否想让TextBox可见当点击前两个RadioButtons中的一个时,如果是这样,你需要使用双向数据绑定,之后你需要更新你的DataSource以显示更新,我已经创建了以下
示例,请尝试检查它:
在MainPage.xaml中:
I wonder if you want to make the TextBox visible when the one of the first two RadioButtons are clicked, if so you need to use the Two-Way data binding, after that you need to update your DataSource to show the update, I have created the following example, please try to check it:
In the MainPage.xaml:
<ListBox Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Tap="StackPanel_Tap" Orientation="Vertical" Background="Red">
<RadioButton Content="RadioButton1" IsChecked="{Binding IsChecked1,Mode=TwoWay}"></RadioButton>
<RadioButton Content="RadioButton2" IsChecked="{Binding IsChecked2,Mode=TwoWay}"></RadioButton>
<RadioButton Content="RadioButton3" IsChecked="{Binding IsChecked3,Mode=TwoWay}"></RadioButton>
<RadioButton Content="RadioButton4" IsChecked="{Binding IsChecked4,Mode=TwoWay}"></RadioButton>
<TextBox Text="I am showing" Visibility="{Binding Visible,Mode=TwoWay}"></TextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在MainPage.xaml.cs中:
In the MainPage.xaml.cs:
public class modelclass:INotifyPropertyChanged
{
private bool _IsChecked1;
private bool _IsChecked2;
private bool _IsChecked3;
private bool _IsChecked4;
private Visibility _Visible;
public bool IsChecked1
{
get { return _IsChecked1; }
set
{
if (value == _IsChecked1) return;
_IsChecked1 = value;
OnPropertyChanged("IsChecked1");
}
}
public bool IsChecked2
{
get { return _IsChecked2; }
set
{
if (value == _IsChecked2) return;
_IsChecked2 = value;
OnPropertyChanged("IsChecked2");
}
}
public bool IsChecked3
{
get { return _IsChecked3; }
set
{
if (value == _IsChecked3) return;
_IsChecked3 = value;
OnPropertyChanged("IsChecked3");
}
}
public bool IsChecked4
{
get { return _IsChecked4; }
set
{
if (value == _IsChecked4) return;
_IsChecked4 = value;
OnPropertyChanged("IsChecked4");
}
}
public Visibility Visible
{
get { return _Visible; }
set
{
if (value == _Visible) return;
_Visible = value;
OnPropertyChanged("Visible");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public partial class MainPage : PhoneApplicationPage
{
public ObservableCollection<modelclass> myModelList;
// Constructor
public MainPage()
{
InitializeComponent();
myModelList=new ObservableCollection<modelclass>();
for(int i=0;i<5;i++)
{
myModelList.Add(new modelclass()
{
IsChecked1 = false,
IsChecked2 = false,
IsChecked3 = false,
IsChecked4 = false,
Visible = Visibility.Collapsed
});
}
MyListBox.ItemsSource = myModelList;
}
private void StackPanel_Tap(object sender, GestureEventArgs e)
{
foreach (var modelclass in myModelList)
{
if (modelclass.IsChecked1 == true || modelclass.IsChecked2 == true)
{
modelclass.Visible = Visibility.Visible;
}
else modelclass.Visible=Visibility.Collapsed;
}
MyListBox.ItemsSource = myModelList;
}
}
最好的问候,
Amy Peng
Best Regards,
Amy Peng
这篇关于[WPSL]将多个控件绑定到WP8 MVVM中的单个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!