I develop a game on Windows Universal App in C#. I have on my interface four buttons (left, right, up, down) to move the character on my map.
My question is : how to activate my function Move() with the keyboard arrows too ?
I tried a lot of solution from the web to get keys are pressed but most oh them concern only input forms...
You can use KeyDown to get the keyboard active.
The xaml is
<Page
x:Class="ktbkwbconcern.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ktbkwbconcern"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" KeyDown="Grid_OnKeyDown">
<Button x:Name="btn" Content="K">
<Button.RenderTransform>
<CompositeTransform></CompositeTransform>
</Button.RenderTransform>
</Button>
<Grid VerticalAlignment="Bottom">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="left" Click="Button_OnClick"></Button>
<Button Grid.Row="1" Grid.Column="0" Content="up" Click="Button_OnClick"></Button>
<Button Grid.Row="0" Grid.Column="1" Content="down" Click="Button_OnClick"></Button>
<Button Grid.Row="1" Grid.Column="1" Content="right" Click="Button_OnClick"></Button>
</Grid>
</Grid>
</Page>
It will move the btn use button up and down.
And you should write code :
private void Grid_OnKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Left)
{
Move(-1, 0);
}
else if (e.Key == VirtualKey.Right)
{
Move(1, 0);
}
else if (e.Key == VirtualKey.Up)
{
Move(0, -1);
}
else if (e.Key == VirtualKey.Down)
{
Move(0, 1);
}
}
private void Move(int x, int y)
{
var temp = btn.RenderTransform as CompositeTransform;
temp.TranslateX += x;
temp.TranslateY += y;
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
var b = sender as Button;
if (b != null)
{
string str = b.Content as string;
if (str == "up")
{
Move(0, -1);
}
else if (str == "down")
{
Move(0, 1);
}
else if (str == "left")
{
Move(-1, 0);
}
else if (str == "right")
{
Move(1, 0);
}
}
}
}
You should use the Grid.KeyDown to get the key and make btn to move.
If have no notion of the code ,please talk me.
Related
I been working with winforms, so my knowledge of WFP is non existent, this is something i am trying to test.
In code I am generating few buttons and placing them on Canvas. Than after clik on any button, i am moving that button around, and after second click button should stay at the position where mouse cursor was when clicked.
If mouse cursor go outside canvas then button will stop follow it.
My problem is, that button is moving, but only when mouse cursor is over that button or any other control, but it is not moving while mouse cursor is traveling over Canvas.
XAML
<Window x:Class="WpfTestDrag.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTestDrag"
mc:Ignorable="d"
Title="MainWindow" Height="522" Width="909">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="130*"/>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="40" />
<RowDefinition Height="*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Canvas Grid.Column="1" Grid.Row="1" x:Name="cnvTest" Width="auto" Height="auto" PreviewMouseMove="CnvTest_PreviewMouseMove"/>
<TextBlock x:Name="txbStatus" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2"/>
</Grid>
</Window>
C#
public partial class MainWindow : Window
{
Button bts;
Boolean isUsed = false;
public MainWindow()
{
InitializeComponent();
CreateButtons();
}
private void CreateButtons()
{
var xPos = 10.0;
var yPos = 15.0;
var Rnd = new Random();
for (int i = 0; i < 3; i++)
{
var btn = new Button();
btn.Name = "btn" + i;
btn.Content = "Button - " + i;
btn.Tag = "Tag" + i;
btn.Width = 150;
btn.Height = 150;
btn.Click += Btn_Click;
Canvas.SetLeft(btn, xPos);
Canvas.SetTop(btn, yPos);
cnvTest.Children.Add(btn);
xPos = xPos + btn.Width + Rnd.Next(-15,40);
yPos = yPos + btn.Height + Rnd.Next(-15, 40);
}
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
bts = sender as Button;
if (isUsed == false)
{
isUsed = true;
}
else
{
isUsed = false;
}
}
private void CnvTest_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point p = Mouse.GetPosition(cnvTest);
if (isUsed == true)
{
Canvas.SetLeft(bts, p.X);
Canvas.SetTop(bts, p.Y);
txbStatus.Text = bts.Name.ToString() + " isUsed:" + isUsed.ToString() + " -> xPos:" + p.X.ToString() + " yPos:" + p.Y.ToString();
}
}
}
Should I use something else than Canvas for this?
You should set the Background property of the Canvas to Transparent (or any other Brush) for it to respond to the mouse events:
<Canvas Grid.Column="1" Grid.Row="1" x:Name="cnvTest" Width="auto" Height="auto" PreviewMouseMove="CnvTest_PreviewMouseMove"
Background="Transparent"/>
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);
}
I'm quite new to WPF and am trying to build a GUI in Blend, but I'm running into all sorts of issues.
I want my finished product to look like this: https://imgur.com/a/BU8Te
(the red is just a placeholder for icons).
Here is the XAML I've got so far:
<Window x:Class="GUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GUI"
mc:Ignorable="d"
Title="GUI"
Height="840"
Width="920"
WindowStyle="none"
ResizeMode="CanResizeWithGrip"
AllowsTransparency="true"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
WindowStartupLocation="CenterScreen"
Background="Transparent"
MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}">
<Border x:Name="shadow">
<Grid x:Name="grid" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="100"/>
<RowDefinition Height="2"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="#F9F9F9" />
<Border Grid.Row="1" Background="#F9F9F9" />
<Border Grid.Row="2" Background="#E9E9E9" />
<DockPanel Grid.Row="0" LastChildFill="False">
<Button x:Name="btnClose"
DockPanel.Dock="Right"
VerticalAlignment="Center"
Height="20" Width="20"
Click="btnClose_Click"
Style="{DynamicResource CloseButton}">
<Path Data="m 357.0883 499.0572 12.62375 12.6275 5.31375 -5.31625 -12.62625 -12.62625 12.62625 -12.61875 -5.31375 -5.3125 -12.62375 12.62 -12.6325 -12.62 -5.30375 5.3125 12.6175 12.61875 -12.6175 12.62625 5.30375 5.31625 12.6325 -12.6275 z" Stretch="Uniform" Fill="#FFAAAAAA" Width="10" Margin="0,0,0,0" ></Path>
</Button>
<Button x:Name="btnMaximise"
DockPanel.Dock="Right"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="20" Width="20"
Click="btnMaximise_Click"
Style="{DynamicResource TitleButton}">
<Path Data="M4.3685131,23.127279L4.3685131,47.283243 47.117023,47.283243 47.117023,23.127279z M0,10.684L53.755001,10.684 53.755001,51.668001 0,51.668001z M8.5679998,0L58.668022,0 64,0 64,5.6864691 64,45.317999 58.668022,45.317999 58.668022,5.6864691 8.5679998,5.6864691z"
Stretch="Uniform" Fill="#FFAAAAAA" Width="10" Margin="0,0,0,0" ></Path>
</Button>
<Button x:Name="btnMinimise"
HorizontalAlignment="Center"
VerticalAlignment="Center"
DockPanel.Dock="Right"
Height="20" Width="20"
Click="btnMinimise_Click"
VerticalContentAlignment="Bottom"
Style="{DynamicResource TitleButton}">
<Button.Content>
<Path Data="M0,20L53.333,20 53.333,8.888 0,8.888z"
Stretch="Uniform" Fill="#FFAAAAAA" Width="10" Margin="0,0,0,5"></Path>
</Button.Content>
</Button>
</DockPanel>
</Grid>
</Border>
</Window>
But when I maximise the window, the top left corner is off my screen (-15, -15 maybe?) and the bottom right corner is also too far up and left because of this. I modified some code that I found online (can't remember the source) which fixed this:
private bool isMaximized;
private Rect normalBounds;
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Maximized && !isMaximized)
{
// max
WindowState = WindowState.Normal;
isMaximized = true;
normalBounds = RestoreBounds;
Height = SystemParameters.WorkArea.Height;
MaxHeight = Height;
MinHeight = Height;
Top = 0;
Left = 0;
Width = SystemParameters.WorkArea.Width;
SetMovable(false);
}
else if (WindowState == WindowState.Maximized && isMaximized)
{
// min
WindowState = WindowState.Normal;
isMaximized = false;
MaxHeight = Double.PositiveInfinity;
MinHeight = 0;
Top = normalBounds.Top;
Left = normalBounds.Left;
Width = normalBounds.Width;
Height = normalBounds.Height;
SetMovable(true);
}
}
private void SetMovable(bool enable)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if (enable)
source.RemoveHook(WndProc);
else
source.AddHook(WndProc);
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch (msg)
{
case WM_SYSCOMMAND:
int command = wParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
handled = true;
break;
}
return IntPtr.Zero;
}
Now that works perfectly, but I'm really struggling to get this to work with a drop shadow on the window. When I add a 10px border to the window and set the dropshadow on this, I get the resizing icon in my border and not in my window:
https://imgur.com/a/S9oLK
I also had to modify the StateChanged event method to hide the border when maximised:
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Maximized && !isMaximized)
{
// max
WindowState = WindowState.Normal;
isMaximized = true;
normalBounds = RestoreBounds;
Height = SystemParameters.WorkArea.Height;
MaxHeight = Height;
MinHeight = Height;
Top = 0;
Left = 0;
Width = SystemParameters.WorkArea.Width;
SetMovable(false);
this.grid.Margin = new Thickness(0);
this.BorderThickness = new Thickness(0);
}
else if (WindowState == WindowState.Maximized && isMaximized)
{
// min
WindowState = WindowState.Normal;
isMaximized = false;
MaxHeight = Double.PositiveInfinity;
MinHeight = 0;
Top = normalBounds.Top;
Left = normalBounds.Left;
Width = normalBounds.Width;
Height = normalBounds.Height;
SetMovable(true);
this.grid.Margin = new Thickness(10);
this.BorderThickness = new Thickness(10);
}
}
But this obviously doesn't work when the window is snapped to the edge of a screen: https://i.imgur.com/pGC1fio.png
To summarise, my issues are:
Resize grabber is in border not main window.
I don't know how to hide the border when the window is snapped to the edge of the screen.
Is there a simple way to fix this? I feel like I'm having to create a very hacky fix for all of this and thought it would be a lot easier to implement a custom window like this.
Thanks in advance for any help.
I have run into a problem with the listpicker. I have a settings page with a option called "Decimals" where you can toggle a "Toggleswitch" and if you turn the toggle on, the listpicker gets enabled. Now when you click on the listpicker, you can choose from 1 to 5 as the amount of decimals. If you choose for example the number "3" in the listpicker, it will get saved to a key on isolated storage with the value 3, if you go to the MainPage it will check the isolatedstorage and if it contains the value "3", it will set a textblock to use 3 decimals. If I then go to my Settings page again, the app crashed with the message "SelectedIndex must always be set to a valid value." Where it should have showed the correct value that was chosen in the listpicker
Here is the code I use:
Settings.XAML:
<phone:PhoneApplicationPage
xmlns:local="clr-namespace:Vaterpas"
x:Class="Vaterpas.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<local:Decimals x:Key="Decimals"/>
<DataTemplate x:Name="lpkFullItemTemplate">
<TextBlock FontSize="36" Text="{Binding}" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="100"/>
<RowDefinition Height="120"/>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="285">
</ColumnDefinition>
<ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.ColumnSpan="2" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="Indstillinger" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid Grid.Column="2" Grid.Row="2">
<toolkit:ToggleSwitch Click="tglDecimals_Click" VerticalAlignment="Top" Height="90" Name="tglDecimals" Content="" Margin="0,10,10,0"></toolkit:ToggleSwitch>
<Grid DataContext="{StaticResource Decimals}">
<toolkit:ListPicker x:Name="lpkDecimals" ItemsSource="{Binding decimals}" FullModeHeader="Antal decimaler" FullModeItemTemplate="{StaticResource lpkFullItemTemplate}" ExpansionMode="FullScreenOnly" IsEnabled="False" Margin="130,60,20,0" SelectionChanged="lpkDecimals_SelectionChanged"></toolkit:ListPicker>
</Grid>
</Grid>
Settings.cs
namespace Vaterpas
{
public partial class Settings : PhoneApplicationPage
{
protected IsolatedStorageSettings m_Settings = IsolatedStorageSettings.ApplicationSettings;
protected const string TOGGLE_DECIMALS_SETTING_KEY = "ToggleDecimals";
protected const string DECIMALS_SETTING_KEY = "Decimals";
public Settings()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (m_Settings.Contains(TOGGLE_DECIMALS_SETTING_KEY))
{
string decimalsToggleValue = (string)m_Settings[TOGGLE_DECIMALS_SETTING_KEY];
string decimalsValue = (string)m_Settings[DECIMALS_SETTING_KEY];
if (decimalsToggleValue == "On")
{
lpkDecimals.IsEnabled = true;
tglDecimals.IsChecked = true;
tblDecimals.Text = "On";
if (decimalsValue == "1")
{
lpkDecimals.SelectedIndex = 0; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "2")
{
lpkDecimals.SelectedIndex = 1; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "3")
{
lpkDecimals.SelectedIndex = 2; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "4")
{
lpkDecimals.SelectedIndex = 3; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "5")
{
lpkDecimals.SelectedIndex = 4; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
}
else
{
tglDecimals.IsChecked = false;
tblDecimals.Text = "Off";
lpkDecimals.IsEnabled = false;
}
}
else
{
tglDecimals.IsChecked = false;
tblDecimals.Text = "Off";
}
base.OnNavigatedTo(e);
}
private void tglDecimals_Click(object sender, RoutedEventArgs e)
{
if (tglDecimals.IsChecked == true)
{
lpkDecimals.IsEnabled = true;
tblDecimals.Text = "On";
m_Settings[TOGGLE_DECIMALS_SETTING_KEY] = "On";
lpkDecimals.SelectedIndex = 0;
m_Settings[DECIMALS_SETTING_KEY] = "1";
}
else
{
lpkDecimals.IsEnabled = false;
tblDecimals.Text = "Off";
m_Settings[TOGGLE_DECIMALS_SETTING_KEY] = "Off";
m_Settings[DECIMALS_SETTING_KEY] = "0";
}
}
private void lpkDecimals_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Make sure we don't handle the event during initiation.
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
if (this.lpkDecimals.SelectedItem != null)
{
m_Settings[DECIMALS_SETTING_KEY] = lpkDecimals.SelectedItem.ToString();
}
}
}
}
public class Decimals
{
public IEnumerable<string> decimals { get { return "1,2,3,4,5".Split(','); } }
}
}
ERROR: SelectedIndex must always be set to a valid value.
I really hope there is someone that can help me with this problem.
I think the the ItemSource is taking time to get assigned to the listpicker as it is assigned in xaml and OnNavigatedTo sometimes runs before InitializeComponent(); method which loads the xaml, and Selected Index property is getting assigned in OnNavigatedTo which is the reason for your error.
Remove the assignment of itemsource from .xaml and Try assigning it in OnNavigatedTo itself before all your coded stuff.
Like this :-
protected override void OnNavigatedTo(NavigationEventArgs e)
{
tglDecimals.ItemSource=Your List;
if (m_Settings.Contains(TOGGLE_DECIMALS_SETTING_KEY))
{
string decimalsToggleValue = (string)m_Settings[TOGGLE_DECIMALS_SETTING_KEY];
string decimalsValue = (string)m_Settings[DECIMALS_SETTING_KEY];
if (decimalsToggleValue == "On")
{
lpkDecimals.IsEnabled = true;
tglDecimals.IsChecked = true;
tblDecimals.Text = "On";
if (decimalsValue == "1")
{
lpkDecimals.SelectedIndex = 0; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "2")
{
lpkDecimals.SelectedIndex = 1; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "3")
{
lpkDecimals.SelectedIndex = 2; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "4")
{
lpkDecimals.SelectedIndex = 3; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "5")
{
lpkDecimals.SelectedIndex = 4; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
}
else
{
tglDecimals.IsChecked = false;
tblDecimals.Text = "Off";
lpkDecimals.IsEnabled = false;
}
}
else
{
tglDecimals.IsChecked = false;
tblDecimals.Text = "Off";
}
base.OnNavigatedTo(e);
}
This Would Help
First have a property for selected index just to check like this--
int lpkSelectedIndex=0;
public int LpkSelectedIndex
{
get
{
return this.lpkSelectedIndex;
}
set
{
this.lpkSelectedIndex= value;
}
}
After that bind this property in xaml like this.And remember bind this after Itemsource property and try it.
<toolkit:ListPicker x:Name="lpkDecimals" ItemsSource="{Binding decimals}" FullModeHeader="Antal decimaler" FullModeItemTemplate="{StaticResource lpkFullItemTemplate}" ExpansionMode="FullScreenOnly" IsEnabled="False" Margin="130,60,20,0" SelectionChanged="lpkDecimals_SelectionChanged" SelectedIndex="{Binding LpkSelectedIndex, Mode=TwoWay}"></toolkit:ListPicker>
I have silverlight web app.I am showing log information in child windows.child window contain a textbox control.I have set ScrollViewer.VerticalScrollBarVisibility="Auto" but vertical scroll bar is not showing up.please help me on this.
XAML
<controls:ChildWindow x:Class="LogPopUpWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="600" Height="400"
Title="" HasCloseButton="False">
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox x:Name="LogEvents" IsReadOnly="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Visible"></TextBox>
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
</Grid>
C#
public void RefreshLogs(string message = "")
{
StringBuilder text = new StringBuilder();
if (string.IsNullOrEmpty(message))
{
if (Logger.GetLogs() != null)
{
Logger.GetLogs().ForEach(b =>
{
text.AppendFormat("{2}{0}: {1}{2}", b.UserTargetOperation, b.UserEventDate.ToString(), Environment.NewLine);
foreach (KeyValuePair<string, string> pair in b.Parameters)
{
text.AppendFormat(" {0} : {1}{2}", pair.Key, pair.Value, Environment.NewLine);
}
});
}
LogEvents.Text = text.ToString();
}
else
{
LogEvents.Text = message;
LogEvents.TextWrapping = TextWrapping.Wrap;
}
}
Button Handler Coder
private void ShowLogLink_Click(object sender, System.Windows.RoutedEventArgs e)
{
///Logger.GetLogs();
///
LogPopUpWindow win = new LogPopUpWindow();
win.RefreshLogs();
win.Show();
}
I would put this just in a comment but I don't have enough rep. I tried to reproduce the error you are describing of that the Vertical Scroll bar is not displaying, but when I fill the textbox with more text than it's height holds, the scroll bar shows.
Is there any other portions influencing your problem that you did not list?
Issue is resolved.I added vertical scroll properties in the code and it is working.
public void RefreshLogs(string message = "")
{
StringBuilder text = new StringBuilder();
if (string.IsNullOrEmpty(message))
{
if (Logger.GetLogs() != null)
{
Logger.GetLogs().ForEach(b =>
{
text.AppendFormat("{2}{0}: {1}{2}", b.UserTargetOperation, b.UserEventDate.ToString(), Environment.NewLine);
foreach (KeyValuePair<string, string> pair in b.Parameters)
{
text.AppendFormat(" {0} : {1}{2}", pair.Key, pair.Value, Environment.NewLine);
}
});
}
LogEvents.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; // added
LogEvents.Text = text.ToString();
}
else
{
**LogEvents.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;** // added
LogEvents.Text = message;
LogEvents.TextWrapping = TextWrapping.Wrap;
}
}
}