I have a simple TextBox control inside a complex UserControl
where I'd like to drop files from Windows Explorer.
Here is the XAML code:
<TextBox x:Name="txtMessage"
AcceptsReturn="True"
AcceptsTab="True"
AllowDrop="True"
PreviewDragOver="TxtMessage_OnDragEnter"
PreviewDragEnter="TxtMessage_OnDragEnter"
PreviewDrop="TxtMessage_OnDrop"
IsEnabled="{Binding IsMessageFieldEnabled}"
Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
FontSize="14">
<TextBox.InputBindings>
<KeyBinding Key="Enter"
Command="{Binding CommandSendMessage}">
</KeyBinding>
</TextBox.InputBindings>
</TextBox>
and here is the code behind:
private void TxtMessage_OnDrop(object sender, DragEventArgs e)
{
IDragAndDrop dd = DataContext as IDragAndDrop;
if (dd == null)
{
return;
}
dd.TryDrop(e.Data);
e.Handled = true;
}
private void TxtMessage_OnDragEnter(object sender, DragEventArgs e)
{
IDragAndDrop dd = DataContext as IDragAndDrop;
if (dd == null)
{
e.Effects = DragDropEffects.None;
return;
}
e.Effects = dd.CheckForAllowedDragDrop(e.Data);
e.Handled = true;
}
The problem is that my code behind is not called at all. Why this might happen? What would prohibit drag & drop stuff to work on a certain control?
Related
I want to make all check boxes selected by selecting check box names with "All of Above".
the checkboxes are in a List box
<ListBox SelectionMode="Multiple"
BorderThickness="0"
ItemsSource="{Binding QuestionThreeSelection}"
SelectedItem="{Binding QuestionThreeSelection}"
Name="listBoxList"
SelectionChanged="listBoxList_SelectionChanged">
<ListBox.InputBindings>
<KeyBinding Command="ApplicationCommands.SelectAll"
Modifiers="Ctrl"
Key="A" />
</ListBox.InputBindings>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Checked="CheckBox_Checked_1"
Content="{Binding SourceName}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
back Code
private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
{
var oo = listBoxList;
CheckBox cb = (CheckBox)sender;
//var w=e;
IEnumerable<AddSource> listallityem = ((IEnumerable<AddSource>)listBoxList.Items.SourceCollection).Where(r => r.IsSelected == false);
//IEnumerable<AddSource> listallityem1 = ((IEnumerable<AddSource>)listBoxList.Items.SourceCollection);
AddSource vv = cb.DataContext as AddSource;
if ((bool) cb.IsChecked)
{
}
if (vv.SourceName== "All of the above")
{
r = listBoxList.ItemsSource;
foreach (AddSource item in wer)
{
item.IsSelected = true; // false in case of unselect
}
}
}
Can someone suggest a method?
You could handle the Checked and Unchecked event for your "All of Above" CheckBox something like this:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
SelectAll(true);
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
SelectAll(false);
}
private void SelectAll(bool select)
{
var all = listBoxList.ItemsSource as IEnumerable<AddSource>;
if (all != null)
{
foreach (var source in all)
source.IsSelected = select;
}
}
Make sure that your AddSource class implements the INotifyPropertyChanged and raise the PropertyChanged event in the setter of the IsSelected property.
I'm trying to code my ESurvey application. I have a list of radio buttons. Here is the code on SurveyPage1.xaml:
<StackPanel Grid.Row="1" Margin="-10,10,10,-10">
<TextBlock Text="PARLIAMENT VISITOR CENTRE SURVEY" FontWeight="Bold" FontSize="48" Margin="100,0,0,0" VerticalAlignment="Top"/>
<TextBlock Text="1. How would you describe your overall visit experience?" Margin="100,100,0,0" FontSize="18"/>
<RadioButton Name="radioExcellent" Content="Excellent" Margin="100,10,0,0" FontSize="12"/>
<RadioButton Name="radioGood" Content="Good" Margin="100,10,0,0" FontSize="12"/>
<RadioButton Name="radioAverage" Content="Average" Margin="100,10,0,0" FontSize="12"/>
<RadioButton Name="radioPoor" Content="Poor" Margin="100,10,0,0" FontSize="12"/>
</StackPanel>
<Button Name="nextButton1" Content="Next" HorizontalAlignment="Left" Margin="1250,525,0,0" Grid.Row="1" VerticalAlignment="Center" Background="#FFED2E38" Click="Button_S1_S2"/>
And my SurveyPage1.xaml.cs:
private void Button_S1_S2(object sender, RoutedEventArgs e)
{
if (this.Frame != null)
{
this.Frame.Navigate(typeof(SurveyPage2));
}
}
What I want to do is that if no radio button is checked, it will not proceed to the next page(clicking nextButton1 would show an error saying "Please enter an option.")
Can someone help me on this? Much appreciated!
You can just check the IsChecked property of all the RadioButtons in a nextButton1 click handler:
private async void Button_S1_S2(object sender, RoutedEventArgs e)
{
if (radioExcellent.IsChecked != true && radioGood.IsChecked != true && radioAverage.IsChecked != true && radioPoor.IsChecked != true)
{
MessageDialog md = new MessageDialog("Please rate the content before proceeding!");
await md.ShowAsync();
}
else
{
if (this.Frame != null)
{
this.Frame.Navigate(typeof(SurveyPage2));
}
}
}
In the click event handler for the next button, you check if some of the radiobuttons are set
Im tweaking a Sound Player app to try and include a Save as Ringtone function. App uses Tiles and a Viewmodel. Tapping each tile plays a sound. I added a Context Menu to the Data Template to give the option on a Tap and Hold event to save that sound as a Ringtone. I am having some issues figuring out just how to use the same source as the Tiles use to play the sound. Below, first code is portion of the Mainpage.xaml. Then the c# code. What I have set at the bottom of the MainPage.cs for the _customRingtone Source is wrong. The emulator stops at "SoundData data = selector.SelectedItem as SoundData;" I cant figure out how to do the source in a similar way the Tile taps get the audio for playing it. I didnt post the ViewModel but can if you want me to. That is where the Tile Groups and sounds are loaded.
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="SoundTileDataTemplate">
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Click="Save_Click" Header="Save as Ringtone" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid Background="{StaticResource PhoneAccentBrush}"
Margin="0, 0, 12, 12">
<Grid VerticalAlignment="Top"
HorizontalAlignment="Right"
Width="40"
Height="40"
Margin="0, 6, 6, 0">
<Ellipse Stroke="{StaticResource PhoneForegroundBrush}"
StrokeThickness="3" />
<Image Source="/Assets/AppBar/Play.png" />
</Grid>
<StackPanel VerticalAlignment="Bottom">
<TextBlock Text="{Binding Title}" Margin="6, 0, 0, 6" />
</StackPanel>
</Grid>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<MediaElement
Name="AudioPlayer"
Volume="1" />
<!--Pivot Control-->
<phone:Pivot Title="{Binding Path=LocalizedResources.ApplicationTitle,
Source={StaticResource LocalizedStrings}}">
<phone:PivotItem Header="{Binding Animals.Title}">
<phone:LongListSelector x:Name="Animal"
SelectionChanged="LongListSelector_SelectionChanged"
Margin="0,0,-12,0"
ItemsSource="{Binding Animals.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
ItemTemplate="{StaticResource SoundTileDataTemplate}"
/>
</phone:PivotItem>
public partial class MainPage : PhoneApplicationPage
{
private readonly SaveRingtoneTask
_CustomRingtone;
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
_CustomRingtone = new SaveRingtoneTask();
_CustomRingtone.Completed +=
customeRingtone_Completed;
BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
private void LongListSelector_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
// verifying our sender is actually a LongListSelector
if (selector == null)
return;
SoundData data = selector.SelectedItem as SoundData;
// verifying our sender is actually SoundData
if (data == null)
return;
private void customeRingtone_Completed(object sender, TaskEventArgs e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show(#"Saved");
}
else if (e.TaskResult == TaskResult.Cancel)
{
MessageBox.Show(#"Canceled");
}
else
{
MessageBox.Show(#"Not Saved");
}
}
private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
SoundData data = selector.SelectedItem as SoundData;
**_CustomRingtone.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute**);
_CustomRingtone.DisplayName = "Ring";
_CustomRingtone.Show();
}
You Save_Click event handler is not passed a LLS, but a context MenuItem. The DataContext of the MenuItem is the object you are after.
private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
{
var element = (FrameworkElement)sender;
SoundData data = element.DataContext as SoundData;
_CustomRingtone.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute**);
_CustomRingtone.DisplayName = "Ring";
_CustomRingtone.Show();
}
I've a list of data,
Each row will show a data and will have a button, when i click the data shown i want give some data to the previous page and when i click the button in the same row i want to send that same data to next page.
My Xaml code,
<ListBox x:Name="List" HorizontalAlignment="Left" Height="612" Margin="6,7,0,0" VerticalAlignment="Top" Width="443" SelectionChanged="List_SelectionChanged_1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="420" Height="50">
<TextBlock x:Name="tbName" Width="400" Height="44" FontSize="22" FontWeight="Bold" Text="{Binding Name}" />
<Button x:Name="DetailButton" Height="44" Width="20" Content=">" FontWeight="Bold" Click="DetailButton_Click_1"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the code for List_SelectionChanged_1 event handler is,
private void List_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
NavigationService.Navigate("/Page1.xaml",selectedItemData);
}
and my DetailButton_Click_1 event handler is,
private void DetailButton_Click_1(object sender, RoutedEventArgs e)
{
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
NavigationService.Navigate("/page3.xaml", selectedItemData);
}
Things work fine for *List_SelectionChanged_1*, but i get an exception while executing
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
of the DetailButton_Click_1 , i get an exception a null exception,
An exception of type 'System.NullReferenceException' occurred in ExpenseApp.DLL but was not handled in user code
What should i do make it work?
The underlying problem is that the sender of the button click event is the button, not the ListBox.
Also note that clicking the button on your data template will not necessarily select that item in the list. Try to grab the clicked item's data context and use that instead of .SelectedItem
private void DetailButton_Click_1(object sender, RoutedEventArgs e)
{
var clickedUIElement = sender as Button;
if (null == clickedUIElement) { Return; }
Display selectedItemData = clickedUIElement.DataContext as Display;
if(null != selectedItemData)
{
NavigationService.Navigate("/page3.xaml", selectedItemData);
}
}
Your code, as it stands, will have a null reference since you can't cast a Button as a ListBox.
try verify if the selectvalue is null before execute the code:
private void DetailButton_Click_1(object sender, RoutedEventArgs e)
{
If ((sender as ListBox).SelectedValue != null){
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
NavigationService.Navigate("/page3.xaml", selectedItemData);
}
}
I was developing Windows Phone App using this sample: Local Database Sample
In that sample, Delete Task has been implemented using an icon. I have modified Delete Task by Context Menu. But, it does not works for me.
If I pressed Delete, nothing happens.
I dunno what mistake I have done.
My modified Code:
XAML Code:
<TextBlock
Text="{Binding ItemName}"
FontWeight="Thin" FontSize="28"
Grid.Column="0" Grid.Row="0"
VerticalAlignment="Top">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="ContextMenu">
<toolkit:MenuItem Name="Delete" Header="Delete" Click="deleteTaskButton_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</TextBlock>
C# Code:
private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
{
// Cast the parameter as a button.
var button = sender as TextBlock;
if (button != null)
{
// Get a handle for the to-do item bound to the button.
ToDoItem toDoForDelete = button.DataContext as ToDoItem;
App.ViewModel.DeleteToDoItem(toDoForDelete);
MessageBox.Show("Deleted Successfully");
}
// Put the focus back to the main page.
this.Focus();
}
Working original Code in that sample:
XAML Code:
<TextBlock
Text="{Binding ItemName}"
FontSize="{StaticResource PhoneFontSizeLarge}"
Grid.Column="1" Grid.ColumnSpan="2"
VerticalAlignment="Top" Margin="-36, 12, 0, 0"/>
<Button
Grid.Column="3"
x:Name="deleteTaskButton"
BorderThickness="0"
Margin="0, -18, 0, 0"
Click="deleteTaskButton_Click">
<Image
Source="/Images/appbar.delete.rest.png"
Height="75"
Width="75"/>
</Button>
C# Code:
private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
{
// Cast the parameter as a button.
var button = sender as Button;
if (button != null)
{
// Get a handle for the to-do item bound to the button.
ToDoItem toDoForDelete = button.DataContext as ToDoItem;
App.ViewModel.DeleteToDoItem(toDoForDelete);
MessageBox.Show("Deleted Successfully");
}
// Put the focus back to the main page.
this.Focus();
}
In your case sender is not a TextBlock, so this line:
var button = sender as TextBlock;
returns null
You can cast it to MenuItem.
using Microsoft.Phone.Controls;
....
private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
{
var item = sender as MenuItem;
if (item!= null)
{
// Get a handle for the to-do item bound to the button.
ToDoItem toDoForDelete = item .DataContext as ToDoItem;
App.ViewModel.DeleteToDoItem(toDoForDelete);
MessageBox.Show("Deleted Successfully");
}
// Put the focus back to the main page.
this.Focus();
}