I try to clear (and add) items to ListPicker, but when the app have to clear all of the items is an error - "SelectedItem must always be set to a valid value". My code:
<toolkit:ListPicker x:Name="select" HorizontalAlignment="Right" Margin="0,135,30,0" VerticalAlignment="Top" Width="195" Height="64" d:LayoutOverrides="HorizontalAlignment" BorderBrush="{x:Null}" FontFamily="Arial" FontWeight="Bold" FontSize="32" Style="{StaticResource ListPickerStyle1}" BorderThickness="0" DataContext="{Binding}">
<toolkit:ListPicker.Background>
<ImageBrush Stretch="Fill" ImageSource="list_picker.png"/>
</toolkit:ListPicker.Background>
and action for a button
private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
select.Items.Clear(); //here is an error
for (int i = 0; i < arrays.Length; i++)
{
select.Items.Add(arrays[i]);
}
}
I try another options, but it doesn't work too.
private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
select.SelectedItem = null; // here is an error
select.Items.Clear();
for (int i = 0; i < arrays.Length; i++)
{
select.Items.Add(arrays[i]);
}
}
To clear the selection you should use:
select.SelectedItems.Clear();
Related
I have a problem, i have 5 buttons that load txt files from system and show it as string on textblocks but i dont know how to do it without 5 event handlers
private void OnClick1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
numbers1.Text = File.ReadAllText(openFileDialog.FileName);
}
OnClick1 is button1, numbers1 is a textblock1
now i have 5 codes like this (with numbers2.Text, numbers3.Text etc) how can i do it shorter
I may be misunderstanding what you are asking. However, if you want the different buttons to use the same click event. Then the click event is going to have to be able to distinguish “which” button was clicked in order to know which text box to use.
In this case, I recommend you give each button a name and then in the click event, cast the sender as a Button, then check its name to determine which text box to use. All buttons are wired up to this ONE (1) event. Example something like…
private void btn_Click(object sender, RoutedEventArgs e) {
Button btnSender = (Button)sender;
TextBox tb = null;
switch (btnSender.Name) {
case "btn1":
tb = txt1;
break;
case "btn2":
tb = txt2;
break;
case "btn3":
tb = txt3;
break;
case "btn4":
tb = txt4;
break;
}
if (tb != null) {
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
tb.Text = File.ReadAllText(openFileDialog.FileName);
}
}
This should get you started I think
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
List<TextBlock> textBlocks = new List<TextBlock>();
textBlocks.Add(txt1);
textBlocks.Add(txt2);
textBlocks.Add(txt3);
textBlocks.Add(txt4);
textBlocks.Add(txt5);
int count = 0;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (String files in openFileDialog.FileNames)
{
var currentText = textBlocks[count];
currentText.Text = File.ReadAllText(files);
count++;
}
}
}
Here is the xaml code
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">
<Button Name="btnLoad" Content="Load All" Click="btnLoad_Click" ></Button>
<TextBlock x:Name="txt1" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt2" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt3" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt4" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt5" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
</StackPanel>
i want to get in my codebehind the Content of an button that has a grid in it with multiple textboxes.
i had this before Code and this works:
XAML:
<Button Click="btnClick_upload_Data">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="test1" ></TextBlock>
<TextBlock Text="test2" ></TextBlock>
</StackPanel>
</Button.Content>
</Button>
codebehind:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = ((((sender as Button).Content) as StackPanel).Children[1] as TextBlock).Text;
//…
and this way i got the "test2" im my string variable.
now my XAML has changed a bit
my Question is how do i have to Change my function so i still get "test2" in my string variable 's'
new XAML:
<Button Click="btnClick_upload_Data" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="test1" Grid.Row="0"></TextBlock>
<TextBlock Text="test2" Grid.Row="1"></TextBlock>
</Grid>
</StackPanel>
</Button.Content>
</Button>
new XAML:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
//????
thanks in advance
Try this:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = null;
Button btn = (Button)sender;
StackPanel sp = btn.Content as StackPanel;
if (sp != null && sp.Children.Count > 0)
{
Grid grid = sp.Children[0] as Grid;
if (grid != null && grid.Children.Count > 1)
{
TextBlock textBlock = grid.Children[1] as TextBlock;
if (textBlock != null)
s = textBlock.Text;
}
}
MessageBox.Show(s);
}
In My project i have a Text Block
<TextBlock Name="MyDisplay" TextAlignment="Center"
Style="{StaticResource HeaderTextBlockStyle}"/>
And Set Of Buttons in a canvas
<Canvas Name="ButtonPanel">
<Button Style="{StaticResource MyButtonStyle}" Content="1" Canvas.Left="0" Canvas.Top="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="2" Canvas.Left="100" Canvas.Top="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="3" Canvas.Left="200" Canvas.Top="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="4" Canvas.Top="86" Canvas.Left="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="5" Canvas.Top="86" Canvas.Left="100"/> </Canvas>
I want to write button content in a text block
My code is
public DisplayPad()
{
this.InitializeComponent();
ButtonPanel.AddHandler(PointerPressedEvent, new PointerEventHandler(ScreenMarkup_PointerPressed), true);
UpdateDisplay();
}
public void ScreenMarkup_PointerPressed(object sender, RoutedEventArgs e)
{
Button button = e.OriginalSource as Button;
if (button == null)
return;
string content = button.Content.ToString();
double digit;
if (double.TryParse(content, out digit))
{
if(content == "1")
{
//codes
MyDisplay.Text = "1";
}
}
UpdateDisplay();
}
void UpdateDisplay()
{
try
{
MyDisplay.Foreground = Application.Current.Resources["ApplicationForegroundThemeBrush"] as Brush;
// Update the display
MyDisplay.Text = String.Format("{0:##.##}");
}
catch
{
//Exception
}
}
But this code is not working it is not update number in TextBlock.
But same thing is used in windows phone 8 which was working fine but only difference in code is (Windows phone 8 code)
public DisplayPad()
{
this.InitializeComponent();
ButtonPanel.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(ScreenMarkupButton_MouseLeftButtonUp), true);
UpdateDisplay();
}
public void ScreenMarkupButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Button button = e.OriginalSource as Button;
if (button == null)
return;
string content = button.Content.ToString();
double digit;
if (double.TryParse(content, out digit))
{
//My Codes
}
UpdateDisplay();
}
The OriginalSource object is not a Button but rather the inner TextBlock within the Button, so you need to change your code to
var textBlock = e.OriginalSource as TextBlock;
if (textBlock == null)
return;
string content = textBlock.Text;
I would like to create a scratch card effect on the Windows Phone. However my current solution seem sluggish and the scratch effect look weird(refer to the screenshot below). The experience is very poor as compared to those that i had seen on iOS.
Would appreciate if someone could guide me toward it. Below are my current code.
<Grid Width="Auto" Height="Auto" Background="Black">
<Viewbox Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid Height="60" Width="60" Background="White">
<InkPresenter x:Name="inkP" Width="60" Height="60">
<InkPresenter.Background>
<ImageBrush Stretch="Fill" ImageSource="/Assets/image.png"/>
</InkPresenter.Background>
<Grid Height="60" Width="60">
<TextBlock x:Name="lblsecretText" HorizontalAlignment="Center" TextWrapping="Wrap" Text="LOL" VerticalAlignment="Center" Width="60" Foreground="Black" TextAlignment="Center" FontSize="5.333"/>
</Grid>
</InkPresenter>
</Grid>
</Viewbox>
</Grid>
Stroke s;
int mycol = 0;
public MainPage()
{
InitializeComponent();
inkP.MouseMove += new MouseEventHandler(inkP_MouseMove);
for (int i = 0; i < 60; i++)
{
for (int l = 0; l < 60; l++)
{
Stroke bigStroke = new Stroke();
bigStroke.StylusPoints.Add(new StylusPoint(i, l));
inkP.Strokes.Add(bigStroke);
}
}
}
StylusPoint _lastPoint;
void inkP_MouseMove(object sender, MouseEventArgs e)
{
StylusPointCollection pointErasePoints = e.StylusDevice.GetStylusPoints(inkP);
pointErasePoints.Insert(0, new StylusPoint(e.GetPosition(inkP).X, e.GetPosition(inkP).Y));
//Compare collected stylus points with the ink presenter strokes and store the intersecting strokes.
StrokeCollection hitStrokes = inkP.Strokes.HitTest(pointErasePoints);
if (hitStrokes.Count > 0)
{
foreach (Stroke hitStroke in hitStrokes)
{
inkP.Strokes.Remove(hitStroke);
}
}
_lastPoint = pointErasePoints[pointErasePoints.Count - 1];
}
Your strokes are beeing removed point by point.
You can adjust this by changing your MainPage constructor to:
public MainPage()
{
InitializeComponent();
inkP.MouseMove += new MouseEventHandler(inkP_MouseMove);
for (int i = 0; i < 60; i++)
{
Stroke bigStroke = new Stroke();
for (int l = 0; l < 60; l++)
{
bigStroke.StylusPoints.Add(new StylusPoint(i, l));
}
inkP.Strokes.Add(bigStroke);
}
}
This will add the strokes line by line.
When you remove them, they will be removed line by line.
Hello I am in need of help to switch the content of two buttons
what I have done so far is to check if the buttons are neighbours.
private int row = 4;
private int col = 4;
public MainWindow()
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button cmd = (Button)sender;
MessageBox.Show(cmd.Tag.ToString());
string txt = cmd.Tag.ToString();
int r = int.Parse("" + txt[0]);
int c = int.Parse("" + txt[1]);
if (Math.Abs(r - row) + Math.Abs(c - col) == 1)
{
MessageBox.Show(r + " " + c);
}
And my buttons in my XAML file is like this
<Button Tag="00" Grid.Row="0" Grid.Column="0" Click="Button_Click">A</Button>
<Button Tag="01" Grid.Row="0" Grid.Column="1" Click="Button_Click">B</Button>
and the Challenge is to switch the content (A and B)
Can anyone help me with that?
do not use WinForms event handling like Button_Click method.
use binding for button content
look at the commands
look at the MVVM approach
Just use a temporary variable to hold one of the strings.
string tmp = Button1.Text;
Button1.Text = Button2.Text;
Button2.Text = tmp;
I'd skip the generic eventhandler name. You need to store the button you want to move to in a temporary variable.
I used nine Buttons:
<Button Tag="00" Grid.Row="0" Grid.Column="0" Margin="15,21,13,23" Name="btn1" Click="btn1_Click">A</Button>
<Button Tag="01" Grid.Row="0" Grid.Column="1" Margin="15,21,13,23" Name="btn2" Click="btn1_Click">B</Button>
<Button Tag="02" Grid.Row="0" Grid.Column="2" Margin="15,21,13,23" Name="btn3" Click="btn1_Click">C</Button>
<Button Tag="10" Grid.Row="1" Grid.Column="0" Margin="15,21,13,23" Name="btn4" Click="btn1_Click">D</Button>
<Button Tag="11" Grid.Row="1" Grid.Column="1" Margin="15,21,13,23" Name="btn5" Click="btn1_Click">E</Button>
<Button Tag="12" Grid.Row="1" Grid.Column="2" Margin="15,21,13,23" Name="btn6" Click="btn1_Click">F</Button>
<Button Tag="20" Grid.Row="2" Grid.Column="0" Margin="15,21,13,23" Name="btn7" Click="btn1_Click">G</Button>
<Button Tag="21" Grid.Row="2" Grid.Column="1" Margin="15,21,13,23" Name="btn8" Click="btn1_Click">H</Button>
<Button Tag="22" Grid.Row="2" Grid.Column="2" Margin="15,21,13,23" Name="btn9" Click="btn1_Click">I</Button>
and This Method to get the Button from the Grid and Change Values:
private int row = 2;
private int col = 2;
private void btn1_Click(object sender, RoutedEventArgs args)
{
Button cmd = (Button)sender;
string txt = cmd.Tag.ToString();
int r = int.Parse("" + txt[0]);
int c = int.Parse("" + txt[1]);
if (Math.Abs(r - row) + Math.Abs(c - col) == 1)
{
MessageBox.Show(r + " " + c);
Button nearButton = grd1.Children.Cast<Button>().First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == col);
Object tmp = nearButton.Content;
nearButton.Content = cmd.Content;
cmd.Content = tmp;
}
}
In my Example the two Buttons next to the specified change their value with it, hope that is what you intended.
(Getting an Item from a Grid via the X and Y is stolen from here)