I have Two radio buttons. If i check first radio button The below data will populate in the combobox. After that i will check another radio button, i want to clear the combo box values.
<RadioButton Height="29"
HorizontalAlignment="Left"
Margin="143,193,0,0" Name="rdoEmployee" VerticalAlignment="Top" Width="61"
FontSize="20" Checked="rdoEmployee_Checked" GroupName="rdoEmployee/>
<RadioButton FontSize="20" Height="20" Margin="228,193,0,0" Name="rdoPA"
VerticalAlignment="Top" HorizontalAlignment="Left" Width="49"
Checked="rdoPA_Checked" GroupName="rdoEmployee />
<ComboBox HorizontalAlignment="Left" Margin="142,235,0,240"
Name="cmbEmpType" Width="200" FontSize="16" />
EmployeeTypes _ET = new EmployeeTypes();
DataRowCollection drc = _ET.EmpTypeTable.Rows;
foreach (DataRow r in drc)
{
ComboBoxItem item = new ComboBoxItem();
item.Tag = r["EmpTypeID"];
item.Content = r["EmpTypeName"];
cmbEmpType.Items.Add(item);
if (cmbEmpType.Items.Count > 0)
{
cmbEmpType.SelectedIndex = 0;
}
}
Are you asking for
cmbEmpType.Items.Clear();
This should empty your combo.
Is it bound? If so, set the bound property to null. If not, set SelectedItem to null.
Related
Not too sure what I am missing here, but I have a few comboBoxes in a form and I would like to hit a "Clear" button and reset all the values to the original SelectedItem value.
xml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="213" Width="513">
<Grid>
<ComboBox x:Name="cbox_Values" HorizontalAlignment="Left" Margin="247,77,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem IsSelected="True">Value1</ComboBoxItem>
<ComboBoxItem>Value2</ComboBoxItem>
<ComboBoxItem>Value3</ComboBoxItem>
</ComboBox>
<Button x:Name="btn_Clear" Content="Clear" HorizontalAlignment="Left" Margin="115,77,0,0" VerticalAlignment="Top" Width="86" Height="21" FontSize="11"/>
</Grid>
</Window>
If I do it this way:
$wpf_btn_Clear.Add_Click({
$wpf_cbox_Values.SelectedItem.Content = "Value1"
})
The current value gets wiped:
If I do it this way:
$wpf_btn_Clear.Add_Click({
$wpf_cbox_Values.SelectedItem.Content = "Value1"
$wpf_cbox_Values.Items.Add("Value2")
$wpf_cbox_Values.Items.Add("Value3")
})
I get doubles:
I am looking for the button to "reset" the comboBox with the original IsSelected item from the xml. For example, if i select Value3, then click the "Clear" button, the value goes back to Value1. This is in Powershell.
$wpf_cbox_Values.SelectedIndex = 0 and/or $wpf_cbox_Values.SelectedItem = $wpf_cbox_Values.Items[0] should work.
ComboBox.SelectedIndex Property
Perhaps try...
$wpf_btn_Clear.Add_Click({
$wpf_cbox_Values.SelectedIndex = 0
})
Should keep your values, but set combobox index to zero which would be your first entry ("Value1").
I have a combo box having four items.
If I select a first item then second combo box must show some item with respect to the selection of first combo box item . If I select the second item then the second combo box must show some item .
Please suggest some ideas.
Add 2 combo box in XAML file:
<ComboBox Name="cbTest1" SelectionChanged="cbTest1_SelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" >
<ComboBoxItem Content="1"></ComboBoxItem>
<ComboBoxItem Content="2"></ComboBoxItem>
<ComboBoxItem Content="3"></ComboBoxItem>
<ComboBoxItem Content="4"></ComboBoxItem>
</ComboBox>
<ComboBox Name="cbTest2" ItemsSource="{Binding Data}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" />
In function cbTest1_SelectionChanged as per selected value you can set value in the variable data
private void cbTest1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<string> oData = new List<string>();
if (((ComboBoxItem)cbTest1.SelectedValue).Content.ToString() == "1")
{
oData.Add("DataType1_1");
oData.Add("DataType1_2");
oData.Add("DataType1_3");
oData.Add("DataType1_4");
}
else if (((ComboBoxItem)cbTest1.SelectedValue).Content.ToString() == "2")
{
oData.Add("DataType2_1");
oData.Add("DataType2_2");
oData.Add("DataType2_3");
oData.Add("DataType2_4");
}
viewModel.Data = oData;
}
I have a very simple sample code, a TextBox and a Button. User inputs some text in textBox and click the Button. It should show a common MessageBox which contains the text he just input in the TextBox.
Problem: the MessageBox shows blank! After some trial and error I found out the reason. I set in .xaml the Focusable of Button as false. Why? Because I want to stop the blinking effect of button after it is clicked once.
So I have an idea. I bind the Focusable to a property in ViewModel and initialized in constructor (or in private field as initial value) of ViewModel as false. Then in the Button-Click event handler before showing MessageBox I set the property to true and then after executing MessageBox set it to false again. Problem: Unfortunately, it doesn't work! It seems the Focusable can only be set once in constructor or in private field as initial value.
Does someone know how to solve this problem? I want to keep the Focusable of the Button to false (to avoid the Button blinking) and I want to get the text from TextBox by clicking the Button. Following is the sample code and feel free to modify and show me the solution. Thank you in advance.
XAML / View
<Grid Width="300" Height="300">
<StackPanel VerticalAlignment="Center">
<TextBox Width="150" Text="{Binding Path=MyText}" />
<Button x:Name="ShowText"
Width="100"
Content="Show Text"
Focusable="{Binding Path=MyFocus}" />
</StackPanel>
</Grid>
View model
public class ShellViewModel : PropertyChangedBase
{
private String _myText;
public String MyText
{
get { return _myText; }
set
{
_myText = value;
NotifyOfPropertyChange(() => MyText);
}
}
private Boolean _myFocus = false; // if it is true, the MessageBox can show MyText, otherwise MessageBox shows blank
public Boolean MyFocus
{
get { return _myFocus; }
set
{
_myFocus = value;
NotifyOfPropertyChange(() => MyFocus);
}
}
public void ShowText()
{
//MyFocus = true; // this doesn't work
MessageBox.Show(MyText);
//MyFocus = false; // this doesn't work
}
}
<Grid Width="300" Height="300">
<StackPanel VerticalAlignment="Center">
<TextBox Width="150" Text="{Binding MyText, Mode=TwoWay}" />
<Button x:Name="ShowText"
Width="100"
Content="Show Text"
Focusable="{Binding MyFocus}" />
</StackPanel>
</Grid>
or
<Grid Width="300" Height="300">
<StackPanel VerticalAlignment="Center">
<TextBox Width="150" x:Name="MyText" />
<Button x:Name="ShowText"
Width="100"
Content="Show Text"
Focusable="{Binding MyFocus}" />
</StackPanel>
</Grid>
that should work either way, the way you did the binding wouldn't ever update the underlying property since it wasn't considered bi-directional. Therefore Mode=TwoWay was necessary on the 1st version, the second version uses CM's conventions to find the the textbox and bind it to a property of the same name on the viewmodel.
not sure what blinking your referring to the default style doesn't have a blink... at least not on windows 8.1 or windows 10. The only thing focusable does in the regard of the code frag you did was prevent keyboard access..
I followed a simple tutorial for comboboxes (http://www.wpf-tutorial.com/list-controls/combobox-control/).
Here is my XAML for the combobox :
<ComboBox Name="CoursesTeach" Grid.Row="7" Grid.Column="1" Width="150" Height="Auto" Margin="0,24">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Foreground="Black" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Code behind :
public AddTrainer()
{
InitializeComponent();
using (Model1Container context = new Model1Container())
{
foreach (var row in context.CourseSet)
{
if (row.Discipline != null)
{
CoursesTeach.ItemsSource = row.Discipline;
}
MetroCustomBox.ShowOK(row.Discipline); // i can see right values
}
}
}
But the results are just NOT in the combobox, although I can perfectly can print them.
Thanks a lot for your responses.
To add items in your Combobox by code behind you may use Items property.
With your previous code :
foreach (var row in context.CourseSet)
{
if (row.Discipline != null)
{
CoursesTeach.Items.Add(row.Discipline);
}
}
But a better way, is use ItemsSource property, with binding, or set by a List.
With your previous code :
CoursesTeach.ItemsSource = context.CourseSet.Where(row => row.Dicipline != null).Select(row => row.Dicipline).ToList();
I have used tab control for view my usercontrols..
in 1st usercontrol
I have used datagrid to diplay Record and for Binding I have used generic List.
When want to change this List as per selected date then that collection is changed in database and in viewmodel also as List's set propery get executes but in view when i selected new tab and then go back to prevois tab at that time List's get property executes & then i am able get view as per selected date.
My main view and which contain 1st usercontrol as 1st tab item is given below:
Xaml code for above view is given below:
<DataGrid
Background="Transparent"
CanUserAddRows="True"
CanUserReorderColumns="False"
ItemsSource="{Binding Model_Transactions_TransactionsDetails_Jama,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
HeadersVisibility="Column">
</DataGrid>
<Grid DockPanel.Dock="Bottom" VerticalAlignment="Bottom" >
<Border BorderBrush="Black" BorderThickness="1" >
<Label HorizontalAlignment="Left" HorizontalContentAlignment="Right" Width="75" Content="{Binding SumOfWeightJama,UpdateSourceTrigger=PropertyChanged}" FontFamily="Segoe UI" FontWeight="Bold" FontSize="16" />
</Border>
</Grid>
<DataGrid
Background="Transparent"
CanUserAddRows="True"
CanUserReorderColumns="False"
ItemsSource="{Binding Model_Transactions_TransactionsDetails_Udhar,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
HeadersVisibility="Column">
</DataGrid>
<Grid DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
<Border BorderBrush="Black" BorderThickness="1">
<Label Width="75" HorizontalAlignment="Left" HorizontalContentAlignment="Right" Content="{Binding SumOfWeightUdhar,UpdateSourceTrigger=PropertyChanged}" FontFamily="Segoe UI" FontWeight="Bold" FontSize="16"/>
</Border>
</Grid>
And View Model for above View is given below:
private DateTime _FilterDate ;
public DateTime FilterDate
{
get
{
return _FilterDate;
}
set
{
_FilterDate = value;
OnPropertyChanged("FilterDate");
Model_Transactions_TransactionsDetails_Jama = (ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionDate.Equals(FilterDate) && s.IsJama).OrderBy(s => s.TransactionsID)).ToList();
Model_Transactions_TransactionsDetails_Udhar = (ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionDate.Equals(FilterDate) && !s.IsJama).OrderBy(s => s.TransactionsID)).ToList();
}
}
public List<Model_TransactionsDetails> Model_Transactions_TransactionsDetails_Jama
{
get
{
return model_Transactions_TransactionsDetails_Jama;
}
set
{
model_Transactions_TransactionsDetails_Jama = value;
OnPropertyChanged("Model_Transactions_TransactionsDetails_Jama");
}
}
public List<Model_TransactionsDetails> Model_Transactions_TransactionsDetails_Udhar
{
get
{
return model_Transactions_TransactionsDetails_Udhar;
}
set
{
model_Transactions_TransactionsDetails_Udhar = value;
OnPropertyChanged("Model_Transactions_TransactionsDetails_Udhar");
}
}
public ViewModel_MasterBook()
{
FilterDate = DateTime.Now.AddDays(-1).Date;
InsertCommand = new RelayCommand(AddExecute, CanAdd);
}
Can any one help me How can i get view as per selected date immediately..
actually it should work i cant see an error. but when i use some kind of Lists in my WPF projects i use observablecollection with clear, add, delete.
but first i would change the binding
ItemsSource="{Binding Model_Transactions_TransactionsDetails_Jama,Mode=OneWay}"
because Mode=TwoWay makes no sense, you never set the itemssource from your datagrid to the viewmodel.
second i would change to ObservableCollection
public ObservableCollection<Model_TransactionsDetails> Model_Transactions_TransactionsDetails_Jama
{
get; private set;
}
with private setter because just initialize once.
//ctor
this.Model_Transactions_TransactionsDetails_Jama = new ObservableCollection<Model_TransactionsDetails>();
and then in your FilterDate setter fill the collection
this.Model_Transactions_TransactionsDetails_Jama.Clear();
var newdata = (ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionDate.Equals(FilterDate) && s.IsJama).OrderBy(s => s.TransactionsID)).ToList();
this.Model_Transactions_TransactionsDetails_Jama.AddRange(newdata);//AddRange is simply an extension method i wrote, you can simply use foreach and .Add()