I have two InkCanvases stacked and I'd like to get the top to pass inking to the bottom. I set the top to ishittestvisible=false but it still receives the inking. Then as a test I set both to false and the top still gets inking...what am I missing?
I created a blank project and it exhibits the same problem. Here's the code:
MainPage.xaml
<RelativePanel>
<StackPanel Name="stackPanelButtons" Orientation="Vertical">
<Button Name="buttonTop" Content="T" Height="50" Click="buttonTop_Click"/>
<Button Name="buttonBottom" Content="B" Height="50" Click="buttonBottom_Click"/>
<Button Name="buttonTopHit" Content="TH" Height="50" Click="buttonTopHit_Click"/>
<Button Name="buttonBottomHit" Content="BH" Height="50" Click="buttonBottomHit_Click"/>
</StackPanel>
<InkCanvas Name="inkCanvasBottom" RelativePanel.RightOf="stackPanelButtons" Width="500" Height="500"/>
<InkCanvas Name="inkCanvasTop" RelativePanel.RightOf="stackPanelButtons" Width="500" Height="500" Loaded="inkCanvasTop_Loaded"/>
</RelativePanel>
MainPage.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void buttonTop_Click(object sender, RoutedEventArgs e)
{
if(buttonTop.Content.Equals("T"))
{
inkCanvasTop.Visibility = Visibility.Collapsed;
buttonTop.Content = "t";
}
else
{
inkCanvasTop.Visibility = Visibility.Visible;
buttonTop.Content = "T";
}
}
private void buttonBottom_Click(object sender, RoutedEventArgs e)
{
if (buttonBottom.Content.Equals("B"))
{
inkCanvasBottom.Visibility = Visibility.Collapsed;
buttonBottom.Content = "b";
}
else
{
inkCanvasBottom.Visibility = Visibility.Visible;
buttonBottom.Content = "B";
}
}
private void buttonTopHit_Click(object sender, RoutedEventArgs e)
{
if (buttonTopHit.Content.Equals("TH"))
{
inkCanvasTop.IsHitTestVisible = false;
buttonTopHit.Content = "Th";
}
else
{
inkCanvasTop.IsHitTestVisible = true;
buttonTopHit.Content = "TH";
}
}
private void buttonBottomHit_Click(object sender, RoutedEventArgs e)
{
if (buttonBottomHit.Content.Equals("BH"))
{
inkCanvasBottom.IsHitTestVisible = false;
buttonBottomHit.Content = "Bh";
}
else
{
inkCanvasBottom.IsHitTestVisible = true;
buttonBottomHit.Content = "BH";
}
}
private void inkCanvasTop_Loaded(object sender, RoutedEventArgs e)
{
addInputs(inkCanvasTop);
addInputs(inkCanvasBottom);
}
private void addInputs(InkCanvas inkCanvas)
{
inkCanvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Mouse;
}
}
Related
I'm trying to create an input box (without buttons) that wait an input from the user (tipically numbers) and, when the lenght is equal to 24, it calls another function to do something.
I already create one but using a confirm button:
in mainpage.xaml.cs
public void WaitingInput()
{
string text = InputTextDialogAsync();
DoSomething(text);
}
private void InputTextDialogAsync()
{
TextBox inputTextBox = new TextBox();
inputTextBox.AcceptsReturn = false;
inputTextBox.Height = 32;
inputTextBox.Width = 300;
ContentDialog dialog = new ContentDialog();
dialog.Content = inputTextBox;
dialog.Title = "Input Reader";
dialog.IsPrimaryButtonEnabled = true;
dialog.IsSecondaryButtonEnabled = false;
dialog.PrimaryButtonText = "Ok";
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
return inputTextBox.Text;
else
return "";
}
it creates what I want but using the ok button to confirm.
public void DoSomething (string text) {
if (text.length < 24) {
WaitingInput();
return;
}
// Do Something with text input ...
}
I tryed with inputTextBox.TextChanging but, even if the input text satisfied the requirements (length == 24) when it finishes the load of the page the inputText appears again...
I'm missing something and maybe it exists a better solution to do that.
---- UPDATE:
Now I'm able to hide the contentdialog box but when you try to insert this input text "123456789012345678901234" I get an error saying that I cannot open more than one content dialog even if I put the Hide function
mainpage.xaml.cs
namespace App1
{
public sealed partial class MainPage : Page
{
ContentDialog dialogInput = new ContentDialog();
TextBox inputBox = new TextBox();
public MainPage()
{
this.InitializeComponent();
}
public void Page_Loaded(object sender, RoutedEventArgs e)
{
WaitingInput();
}
public async void WaitingInput()
{
inputBox.AcceptsReturn = false;
inputBox.Height = 32;
inputBox.Width = 300;
inputBox.TextChanging += TextChangingHandler;
dialogInput.Content = inputBox;
dialogInput.Title = "Input Reader";
dialogInput.IsPrimaryButtonEnabled = false;
dialogInput.IsSecondaryButtonEnabled = false;
dialogInput.PrimaryButtonText = "";
await dialogInput.ShowAsync();
}
private void TextChangingHandler(TextBox sender, TextBoxTextChangingEventArgs e)
{
if (sender.Text.Length < 24)
{
return;
}
dialogInput.Hide();
DoSomething(sender.Text);
}
private async void DoSomething(string inputTextUSER)
{
if (inputTextUSER == "123456789012345678901234")
{
WaitingInput();
return;
}
inputText.Text = inputTextUSER;
await Task.Delay(3000);
}
}
}
mainPage.xaml
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<TextBox x:Name="inputText" Text="" HorizontalAlignment="Center" VerticalAlignment="Top" IsReadOnly="True" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" FontSize="24" FontWeight="Bold" RequestedTheme="Default" IsHitTestVisible="False" IsTabStop="False" IsTextPredictionEnabled="False" BorderThickness="0,0,0,2" BorderBrush="Black" Width="592" TextAlignment="Center" Foreground="Black" Background="Transparent" Height="45" Margin="0,50,0,0" />
</Grid>
I think OnTextChanging() is the way to go if I understand your requirements correctly.
Here is some code... but understand that there are likely other scenarios that should be accounted for... what about pasting data in that is longer than 24? validation on input? how to close the window if there are no buttons and user wants to back out (esc works, but is that good design)? etc. etc. I'm also not getting into MVVM or other concepts like that. There is also no exception handling in this code.
You should be able to modify to your liking.
Input Text Dialog control:
<ContentDialog
x:Class="App1.InputTextDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE">
<Grid>
<TextBox AcceptsReturn="False" Height="32" Width="300" TextChanging="OnTextChanging" />
</Grid>
</ContentDialog>
code behind:
public sealed partial class InputTextDialog : ContentDialog
{
public event EventHandler<EnteredTextArgs> OnValueEntered;
public InputTextDialog()
{
this.InitializeComponent();
}
private void OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (sender.Text.Length == 24)
{
OnValueEntered?.Invoke(this, new EnteredTextArgs() { EnteredText = sender.Text });
sender.Text = string.Empty;
this.Hide();
}
}
}
args class:
public class EnteredTextArgs : EventArgs
{
public string EnteredText { get; set; }
}
main page:
<StackPanel>
<Button Content="Show Dialog" Click="OnShowClick" />
<TextBlock x:Name="txtblockResult" />
</StackPanel>
main page code behind:
public sealed partial class MainPage : Page
{
InputTextDialog dialog = new InputTextDialog();
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
dialog.OnValueEntered += Dialog_OnValueEntered;
}
private void Dialog_OnValueEntered(object sender, EnteredTextArgs e)
{
txtblockResult.Text = e.EnteredText;
}
private async void OnShowClick(object sender, RoutedEventArgs e)
{
await dialog.ShowAsync();
}
}
****UPDATE****
OK. Based on your most recent update, I think this is what you intended... but without knowing the full requirements, this seems like it needs some work. Good luck!
public sealed partial class MainPage : Page
{
ContentDialog dialogInput = new ContentDialog();
TextBox inputBox = new TextBox();
public MainPage()
{
this.InitializeComponent();
//you only want to call this once... not each time you show the dialog
Setup();
Loaded += Page_Loaded;
}
public void Page_Loaded(object sender, RoutedEventArgs e)
{
WaitingInput();
}
/// <summary>
/// initializes the dialog and its child - the textbox
/// </summary>
private void Setup()
{
inputBox.AcceptsReturn = false;
inputBox.Height = 32;
inputBox.Width = 300;
inputBox.TextChanging += TextChangingHandler;
dialogInput.Content = inputBox;
dialogInput.Title = "Input Reader";
dialogInput.IsPrimaryButtonEnabled = false;
dialogInput.IsSecondaryButtonEnabled = false;
dialogInput.PrimaryButtonText = "";
}
private void ResetDialog()
{
inputBox.Text = string.Empty;
WaitingInput();
}
public async void WaitingInput()
{
await dialogInput.ShowAsync();
}
private async void TextChangingHandler(TextBox sender, TextBoxTextChangingEventArgs e)
{
if (sender.Text.Length < 24)
{
return;
}
dialogInput.Hide();
await DoSomething(sender.Text);
}
private async Task DoSomething(string inputTextUSER)
{
if (inputTextUSER == "123456789012345678901234")
{
//note: the dialog will not show again. May as well close the app
return;
}
//show inputted text in textblock
inputText.Text = inputTextUSER;
await Task.Delay(3000);
//after 3 seconds, show the dialog again - unclear requirement
ResetDialog();
}
}
mainpage.xaml:
<Grid>
<TextBox x:Name="inputText" Text="" HorizontalAlignment="Center" VerticalAlignment="Top" IsReadOnly="True" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" FontSize="24" FontWeight="Bold" RequestedTheme="Default" IsHitTestVisible="False" IsTabStop="False" IsTextPredictionEnabled="False" BorderThickness="0,0,0,2" BorderBrush="Black" Width="592" TextAlignment="Center" Foreground="White" Background="Transparent" Height="45" Margin="0,50,0,0" />
</Grid>
I'm trying to create a canvas that takes items from ListView and puts them where the user wants.
Canvas won't fire my DragEnter and Drop events. Here is my code for ListView and Canvas properties.
private void itemList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(null);
isDragging = true;
}
private void itemList_MouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
ListView listView = sender as ListView;
ListViewItem listViewItem =
FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);
Model.Resource student = (Model.Resource)listView.ItemContainerGenerator.
ItemFromContainer(listViewItem);
dragItem = new DDitem(student.getName(), student.getIco());
DataObject dragData = new DataObject("myFormat", dragItem);
DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move);
}
}
Now for my <Canvas>
private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
{
do
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}
private void can_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("myFormat") || sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
Console.WriteLine("Pokusava drag enter");
}
private void can_Drop(object sender, DragEventArgs e)
{
Console.WriteLine("Pokusava drop");
if (e.Data.GetDataPresent("myFormat"))
{
Model.Resource student = e.Data.GetData("myFormat") as Model.Resource;
resources.Remove(student);
isDragging = false;
}
}
My XAML code for Canvas and ListView is below
<Canvas Name="can" Background="Transparent" Margin="10,10,10,0" Grid.Row="1" AllowDrop="True" DragEnter="can_DragEnter" Drop="can_Drop" />
<ListView x:Name="itemList" Grid.Column="1" HorizontalAlignment="Stretch" Margin="7,23,6,0" Grid.Row="1" VerticalAlignment="Stretch" Background="#324251" ItemsSource="{Binding Path=resources}" FontSize="16" Foreground="Wheat" PreviewMouseLeftButtonDown="itemList_PreviewMouseLeftButtonDown" MouseMove="itemList_MouseMove" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ico}" Width="70" Height="70"></Image>
<TextBlock Text="{Binding name}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Output gives no exceptions or errors.
Problem fixed, the problem was that I've put Canvas before Image
Now, I've ran to another problem, I'm trying to add my UserControl called DDItem to canvas, but with no luck. It should follow my mouse pointer and when I release my mouse, it should be put on the canvas. It does get added to my list of dragged items but I cant see it.
XAML code of my DDItem is below
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="61*"/>
<RowDefinition Height="14*"/>
</Grid.RowDefinitions>
<Image x:Name="mask" Source="{Binding Path=src}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill" Grid.Row="0"/>
<Label x:Name="label" Content="{Binding Path=name}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Foreground="Wheat" FontSize="40"/>
</Grid>
Code inside my DDItem class
public partial class DDitem : UserControl
{
private string _name;
private string _path;
private Point position;
public string name
{
get;
set;
}
public string path
{
get;
set;
}
public DDitem(string name, string path)
{
this.name = name;
this.path = path;
this.Width = 100;
this.Height = 100;
this.DataContext=this;
InitializeComponent();
}
public Point getPosition()
{
return position;
}
public void setPosition(Point pos)
{
position = pos;
}
}
Now for my Canvas events:
private void can_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("myFormat") || sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
private void can_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
can.Children.Add(dragItem);
dragItem.Visibility = Visibility.Visible;
Canvas.SetTop(dragItem, dragItem.getPosition().Y);
Canvas.SetLeft(dragItem, dragItem.getPosition().X);
isDragging = false;
resOnCanvas.Add(dragItem);
}
}
private void can_DragOver(object sender, DragEventArgs e)
{
if (dragItem != null)
{
if (!dragItem.IsVisible)
{
dragItem.Visibility = Visibility.Visible;
}
dragItem.setPosition(e.GetPosition(can));
Console.WriteLine(dragItem.getPosition().X + " " + dragItem.getPosition().Y);
}
}
I create a list and set it's datatemplete as Image.
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="411" Margin="45,24,0,0" VerticalAlignment="Top" Width="336">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImgSource}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
then I load 100 pictures
public partial class MainWindow : Window
{
List<test> lstTest = new List<test>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
{
test t = new test();
t.ImgSource = #"d:\test\1.jpg";
lstTest.Add(t);
}
}
private void btnHead_Click(object sender, RoutedEventArgs e)
{
listBox.ItemsSource = lstTest;
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
lstTest = null;
listBox.ItemsSource = null;
}
public class test
{
public string ImgSource { get; set; }
}
}
before I click btnHead,the mem is 20m, and when I click this button, the mem increase to 40m.
I want to create a function to release mem (return 20m), but btnClear_Click do not work.
How to dispose Image in wpf?
I have a WebBrowser control and I want to show some url by parameter on this control. Until the webbrower loaded the page, I need to show some progressbar or animation.
Please help me, here's what I have:
public partial class brw : PhoneApplicationPage
{
public brw()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string parameterValue = NavigationContext.QueryString["parameter"];
System.Uri uri = new System.Uri(parameterValue);
webbrowser.Source = uri;
}
private void WebBrowser_Navigating_1(object sender, Microsoft.Phone.Controls.NavigatingEventArgs e)
{
progressbar.Visibility = Visibility.Visible;
}
private void WebBrowser_Navigated_1(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
progressbar.Visibility = Visibility.Collapsed;
}
private void PhoneApplicationPage_Loaded_1(object sender, System.Windows.RoutedEventArgs e)
{
}
}
Thank you
This can be achieved by using LoadCompleted property.
XAML:
<ProgressBar x:Name="progressbar" IsIndeterminate="True"/>
<phone:WebBrowser x:Name="webbrw" IsScriptEnabled="True" LoadCompleted="yesLoaded"/>
.cs:
private void yesLoaded(object sender, NavigationEventArgs e)
{
this.progressbar.Visibility = Visibility.Collapsed;
this.progressbar.IsIndeterminate = False;
}
Have a look at this sample.
Hope it helps!
try this:
webbrowser.Navigate(new Uri(parameterValue));
instead of
webbrowser.Source = uri;
set Progressbar's Property IsIndeterminate="True"
<ProgressBar Foreground="Orange" x:Name="progressbar" Visibility="Collapsed" IsIndeterminate="True" Height="4" HorizontalAlignment="Left" Margin="10,66,0,0" VerticalAlignment="Top" Width="460" />
I have a small prototype that allows to add selected item from one listbox to another. I need to be able to select multiple item from listbox and move them to another and backward - from second listbox to first one. I am wondering if someone has good sample or can modify the code I already have. Thank you in advance.
Two listboxes and buttons XAML:
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Margin="0,40,225,20">
<ListBox x:Name="LeftListBox" SelectionMode="Multiple"/>
</Border>
<Border Margin="0,40,20,20" BorderThickness="1" BorderBrush="#FFCECECE" HorizontalAlignment="Right" Width="169" Padding="5" >
<ListBox x:Name="RightListBox" BorderThickness="0" />
</Border>
<Button x:Name="AddButton" Height="40" Margin="0,3,198,0" VerticalAlignment="Center" Click="AddButton_Click" HorizontalAlignment="Right" Width="15" Content="▶" />
<Button x:Name="RemoveButton" Height="40" Margin="0,94,198,0" VerticalAlignment="Center" Click="RemoveButton_Click" HorizontalAlignment="Right" Width="15" Content="R" />
</Grid>
Code behind:
public partial class SelectServersUC : UserControl
{
private ArrayList myDataList = null;
string currentItemText ;
int currentItemIndex ;
public SelectServersUC()
{
this.InitializeComponent();
myDataList = LoadListBoxData();
LeftListBox.ItemsSource = myDataList;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
myDataList = LoadListBoxData();
LeftListBox.ItemsSource = myDataList;
}
private ArrayList LoadListBoxData()
{
ArrayList itemsList = new ArrayList();
itemsList.Add("Item1");
itemsList.Add("Item2");
itemsList.Add("Item3");
itemsList.Add("Item4");
itemsList.Add("Item5");
itemsList.Add("Item6");
itemsList.Add("Item7");
itemsList.Add("Item8");
itemsList.Add("Item9");
itemsList.Add("Item10");
return itemsList;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
// Find the right item and it's value and index
currentItemText = LeftListBox.SelectedValue.ToString();
currentItemIndex = LeftListBox.SelectedIndex;
RightListBox.Items.Add(currentItemText);
if (myDataList != null)
{
myDataList.RemoveAt(currentItemIndex);
}
// Refresh data binding
ApplyDataBinding();
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
// Find the right item and it's value and index
currentItemText = RightListBox.SelectedValue.ToString();
currentItemIndex = RightListBox.SelectedIndex;
// Add RightListBox item to the ArrayList
myDataList.Add(currentItemText);
// LeftListBox.Items.Add(RightListBox.SelectedItem);
RightListBox.Items.RemoveAt(RightListBox.Items.IndexOf(RightListBox.SelectedItem));
// Refresh data binding
ApplyDataBinding();
}
/// <summary>
/// Refreshes data binding
/// </summary>
private void ApplyDataBinding()
{
LeftListBox.ItemsSource = null;
// Bind ArrayList with the ListBox
LeftListBox.ItemsSource = myDataList;
}
just have a look at this
Move items from one listbox to another
http://www.c-sharpcorner.com/UploadFile/mahesh/WPFListBoxDataTransfer07272008130032PM/WPFListBoxDataTransfer.aspx
public partial class listtolist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
}