I have a listbox with a textbox
The textbox is defined in a datatemplate that ends like this
<TextBlock Grid.Row="4" Grid.Column="0" Text="Note"></TextBlock>
<TextBox Height="Auto" Grid.Row="4" Grid.Column="1"
Text="{Binding PartData.Note}" AcceptsReturn="True" TextWrapping="Wrap" >
</TextBox>
</Grid>
I want the textbox to expand when the user enters multiple rows but it doesnt. The rowdefintion height is set to *
I've tried your sample with this code, and it works (use Shift-Enter to start a new row inside TextBox)
<Window x:Class="TextBoxWrap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="140" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Note"/>
<TextBox Margin ="10, 0,0,0" Height="Auto" Grid.Row="1" Grid.Column="1" Text="{Binding Count}"
AcceptsReturn="True" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
You need to add
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
to TextBox so it takes all available space.
Related
Right now I have this XAML layout for my WPF application:
<Window x:Class="Cabrillo_Editor.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:Cabrillo_Editor"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Exit" Click="ExitApplication"/>
<MenuItem Header="_New"/>
<MenuItem Header="_Save" Click="SaveCabrilloFile"/>
</Menu>
<StackPanel>
<GroupBox Height="Auto" Header="General">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="185"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" Grid.Column="0" Margin="0,0,0,5">
<Label>My Call:</Label>
<TextBox Width="120" Name="CallsignTextBox" HorizontalAlignment="Right" CharacterCasing="Upper"/>
</DockPanel>
<DockPanel Grid.Row="2" Grid.Column="0">
<Label>My Grid:</Label>
<TextBox Width="120" Name="GridTextBox" HorizontalAlignment="Right"/>
</DockPanel>
<DockPanel Grid.Row="0" Grid.Column="2">
<Label>Contest:</Label>
<ComboBox Width="150" Name="ContestComboBox" Margin="5,0,0,0"/>
</DockPanel>
<DockPanel Grid.Row="2" Grid.Column="2">
<Label>Assisted:</Label>
<CheckBox VerticalAlignment="Center" Name="AssistedCheckBox" Margin="5,0,0,0"/>
</DockPanel>
<DockPanel Grid.Row="0" Grid.Column="4">
<Label>Band:</Label>
<ComboBox Width="150" Name="BandComboBox" Margin="5,0,0,0"/>
</DockPanel>
</Grid>
</GroupBox>
</StackPanel>
</DockPanel>
</Window>
And it looks like this:
Why, for example, is are the ComboBoxes so streched if I set the row height to "Auto" (same for the TextBoxes)?
Is there a better way to make consistent horizontal space between the columns?
This is occurring because those controls will stretch to fill their parent container by default. To override this, simply set the VeriticalAlignment property and Height property (if needed).
<ComboBox Width="150" Name="ContestComboBox" Margin="5,0,0,0" VerticalAlignment=Top/>
i have a ListView with following ItemTemplate
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:SubsceneDownloadModel">
<UserControl PointerEntered="ListViewSwipeContainer_PointerEntered"
PointerExited="ListViewSwipeContainer_PointerExited">
<Grid AutomationProperties.Name="{x:Bind Name}">
<SwipeControl x:Name="ListViewSwipeContainer" >
<Grid VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{x:Bind Name}"
Margin="10,5,10,5"
FontSize="18"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<AppBarButton x:Name="DownloadHoverButton"
Margin="10,0,10,0"
HorizontalAlignment="Right"
IsTabStop="False"
Visibility="Collapsed"
Label="Download"
Icon="Download"
Click="DownloadHoverButton_Click"/>
<ProgressRing x:Name="prgStatus"/>
</Grid>
</SwipeControl>
</Grid>
</UserControl>
</DataTemplate>
</ListView.ItemTemplate>
I want the value of the ProgressRing to change when I click on the AppBarButton but the problem is AppBarButton is not accessible from item template, so how can i access progressring from itemtemplate?
move your itemtemplate to a new usercontrol
<UserControl
Name="subsceneView"
x:Class="HandySub.UserControls.SubsceneUserControl"
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"
mc:Ignorable="d"
PointerEntered="UserControl_PointerEntered"
PointerExited="UserControl_PointerExited">
<Grid>
<SwipeControl x:Name="ListViewSwipeContainer" >
<Grid AutomationProperties.Name="{x:Bind Name}">
<SwipeControl x:Name="ListViewSwipeContainer" >
<Grid VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{x:Bind Name}"
Margin="10,5,10,5"
FontSize="18"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<AppBarButton x:Name="DownloadHoverButton"
Margin="10,0,10,0"
HorizontalAlignment="Right"
IsTabStop="False"
Visibility="Collapsed"
Label="Download"
Icon="Download"
Click="DownloadHoverButton_Click"/>
<ProgressRing x:Name="prgStatus"/>
</Grid>
</SwipeControl>
</Grid>
</SwipeControl>
</Grid>
</UserControl>
and in your listview
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:SubsceneDownloadModel">
<usercontrol:SubsceneUserControl/>
</DataTemplate>
</ListView.ItemTemplate>
I have created a application for windows 10 (uwp) devices. I´m using the windows appstudio "designer". Now, I have to update the source code by a combobox and the correct eventhandler.
The combobox items contains different urls. After the selection changed, I have to parse entries from a url.
Layout-file:
<Page
x:Class="App.Pages.NeuigkeitenListPage"
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:was_actions="using:AppStudio.Uwp.Actions"
xmlns:was_commands="using:AppStudio.Uwp.Commands"
xmlns:was_controls="using:AppStudio.Uwp.Controls"
xmlns:list_layouts="using:Wallfahrtsapp.Layouts.List"
xmlns:controls="App.Layouts.Controls"
xmlns:vm="using:App.ViewModels"
xmlns:triggers="using:App.Triggers"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DataContext="{d:DesignData Source=/Assets/Design/DesignData.json, Type=vm:DesignViewModel, IsDesignTimeCreatable=true}"
mc:Ignorable="d">
<Page.Resources>
<was_controls:VisualBreakpoints x:Name="ResponsiveBehaviorsVBP" ConfigFile="/Assets/ResponsiveBehaviorsVBP.json"></was_controls:VisualBreakpoints>
</Page.Resources>
<Grid Background="{StaticResource AppBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.ColumnSpan="2" Background="{StaticResource AppBarBackground}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="48"/>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="{Binding Active.pageTitleMargin, Source={StaticResource ResponsiveBehaviorsVBP}}" Text="{x:Bind ViewModel.PageTitle}" Foreground="{StaticResource AppBarForeground}" FontSize="{StaticResource AppTitleTextSizeDefault}" VerticalAlignment="Center" HorizontalAlignment="Left" TextTrimming="CharacterEllipsis" MaxLines="1"/>
<was_actions:ActionsCommandBar
x:Name="appBar"
ActionsSource="{x:Bind ViewModel.Actions}" Style="{StaticResource WasCommandBarStyle}"
Foreground="{StaticResource AppBarForeground}"
IsVisible="{x:Bind ViewModel.HasActions}"
Background="{StaticResource AppBarBackground}"
Grid.Row="{Binding Active.appBarRow, Source={StaticResource ResponsiveBehaviorsVBP}}"
Grid.Column="{Binding Active.appBarColumn, Source={StaticResource ResponsiveBehaviorsVBP}}"
Grid.ColumnSpan="{Binding Active.appBarColumnSpan, Source={StaticResource ResponsiveBehaviorsVBP}}" Opened="appBar_Opened">
</was_actions:ActionsCommandBar>
<ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="3"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ProgressBar Grid.Row="1" Height="3" Margin="0,6,0,6" IsIndeterminate="True" Foreground="{StaticResource PageTitleForeground}" Visibility="{x:Bind ViewModel.IsBusy, Converter={StaticResource BoolToVisibilityConverter}, FallbackValue=Collapsed, Mode=OneWay}"/>
<was_controls:ErrorNotificationControl x:Uid="ListErrorNotificationControl" Grid.Row="2" ErrorVisibility="{x:Bind ViewModel.HasLoadDataErrors, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}" ErrorColor="{StaticResource PageTitleForeground}" Margin="10,0,18,0"/>
<list_layouts:ListContactCard Grid.Row="1" ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}" ItemClickCommand="{x:Bind ViewModel.ItemClickCommand}" OneRowModeEnabled="False" Margin="31,0,0,3" Grid.RowSpan="3" />
<ListBox></ListBox>
</Grid>
</ScrollViewer>
<TextBlock HorizontalAlignment="Left"
x:Name="source"
TextWrapping="Wrap"
Text="Newsquelle auswählen:"
VerticalAlignment="Top"
FontFamily="Segoe UI"
FontStyle="Oblique"
Padding="40" Margin="384,-30,0,0" Height="78"
/>
<ComboBox x:Name="sourceComboBox"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Width="200"
SelectionChanged="selectionChanged"
>
<ComboBoxItem Content="Url1"/>
<ComboBoxItem Content="Url2"/>
</ComboBox>
<controls:DataUpdateInformationControl Grid.Row="2" Grid.ColumnSpan="2" LastUpdateDateTime="{x:Bind ViewModel.LastUpdated, Mode=OneWay}" Color="{StaticResource PageTitleForeground}" Margin="8,4,8,4" HorizontalAlignment="Left" HasLocalData="{x:Bind ViewModel.HasLocalData}"/>
</Grid>
C#- file: Eventhandler
private void selectionChanged (object sender , SelectionChangedEventArgs args)
{
index = sourceComboBox.SelectedIndex;
Debug.WriteLine("selectionChangedEventMethod");
newsblogHTTP(index);
}
But how can I update my layout with the new content?
If you need more code snippets let´s see the repo for my project.
layout-file : https://github.com/MasterCoder1992/BlutWallfahrtWindows10/blob/version/march2k17Y/Wallfahrtsapp.W10/Pages/NeuigkeitenListPage.xaml
https://github.com/MasterCoder1992/BlutWallfahrtWindows10/blob/version/march2k17Y/Wallfahrtsapp.W10/Pages/NeuigkeitenListPage.xaml.cs
C#-Config file:
https://github.com/MasterCoder1992/BlutWallfahrtWindows10/blob/version/march2k17Y/Wallfahrtsapp.W10/Sections/NeuigkeitenConfig.cs
Thanks for your help!
I am currently in the process of making a 'Preferences' window in my C# WPF program.
The aim of this window is to have a ListView on the left, and a list of tweakable controls on the right.
Each item in the ListView directly corresponds to a Grid that contains all of the controls under the selected item's content.
Once the item in the ListView is changed, the current grid's visibility must be collapsed, and the grid that corresponds to the item selected's visibility must be changed to visible.
I thought that DataBinding might work for this, however I have no idea how to use it. Could someone please inform me how to implement this feature?
I have only one grid at the moment. The whole window looks like this:
<Window x:Class="DarkOrbit_Skill_Price_Calculator.DarkOrbit_Skill_Price_Calculator___Preferences"
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:DarkOrbit_Skill_Price_Calculator"
mc:Ignorable="d"
Title="DarkOrbit Skill Price Calculator - Preferences" Height="360" Width="640" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListView Name="ListView_PreferenceOption" Width="150" Margin="5" SelectionChanged="SelectionChanged_ListView_PreferenceOption">
<ListViewItem IsSelected="True">
<StackPanel Orientation="Horizontal">
<Image Source="Images\Installing Updates.png" Height="35"/>
<TextBlock Text="Update" FontSize="14" FontFamily="Segoe UI" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</ListViewItem>
</ListView>
<Grid Margin="5" Name="Grid_Update" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Update Version Architecture" VerticalAlignment="Center"/>
<ComboBox IsReadOnly="True" Width="100" Margin="5">
<ComboBoxItem Content="64-Bit"/>
<ComboBoxItem Content="32-Bit" IsSelected="True"/>
</ComboBox>
</StackPanel>
</Grid>
</Grid>
</Window>
I have absolutely no clue as to how to swap the grid depending on the index selected.
You can bind the visibility of each grid to the selected state of its corresponding list item by referencing the ListViewItem element. Something like this:
<Grid>
<Grid.Resources>
<BooleanToVisibilityConverter x:Key="Converter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListView Width="150" Margin="5">
<ListViewItem IsSelected="True" x:Name="One">
<TextBlock Text="Update" FontSize="14" FontFamily="Segoe UI"
VerticalAlignment="Center" Margin="5"/>
</ListViewItem>
<ListViewItem IsSelected="False" x:Name="Two">
<TextBlock Text="Foo" FontSize="14" FontFamily="Segoe UI"
VerticalAlignment="Center" Margin="5"/>
</ListViewItem>
</ListView>
<Grid Margin="5" Grid.Column="1"
Visibility="{Binding IsSelected, ElementName=One, Converter={StaticResource Converter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Update Version Architecture" VerticalAlignment="Center"/>
<ComboBox IsReadOnly="True" Width="100" Margin="5">
<ComboBoxItem Content="64-Bit"/>
<ComboBoxItem Content="32-Bit" IsSelected="True"/>
</ComboBox>
</StackPanel>
</Grid>
<Grid Margin="5" Grid.Column="1"
Visibility="{Binding IsSelected, ElementName=Two, Converter={StaticResource Converter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="Update Something Else" VerticalAlignment="Center"/>
<ComboBox IsReadOnly="True" Width="100" Margin="5">
<ComboBoxItem Content="64-Bit"/>
<ComboBoxItem Content="32-Bit" IsSelected="True"/>
</ComboBox>
</StackPanel>
</Grid>
</Grid>
For those who are having the same predicament and want to use C# code to make this work, I eventually thought of the following code:
StackPanel[] allGrids = { Grid_1, Grid_2, Grid_3, Grid_4, ... }; //Replace StackPanel with the
//type of control you are using, e.g. Grid or WrapPanel.
foreach (StackPanel grid in allGrids)
{
grid.Visibility = Visibility.Collapsed; //collapse all grids
}
allGrids[(your listview/other control).SelectedIndex].Visibility = Visibility.Visible;
//make the grid you need visible
How do I set a wpf text box to resize automatically when the user changes the size of dialog box?
<Window x:Class="MemoPad.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="LightGray"
Title="Window1" Height="350" Width="700" >
<StackPanel Orientation="Vertical">
<Menu DockPanel.Dock ="Right">
<MenuItem Header="Find" x:Name="gMNuFind" />
</Menu>
<Button Content=" Find "
Margin="5,10,5,5"
x:Name="gBuFind"
/>
<TextBox Margin="0,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
MinHeight="270" MinWidth="690"
x:Name = "gTBxInfo"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</StackPanel>
Or change StackPanel to Grid
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="LightGray"
Title="Window1" Height="350" Width="700" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Find" x:Name="gMNuFind" />
</Menu>
<Button Grid.Row="1" Content=" Find "
Margin="5,10,5,5"
x:Name="gBuFind"
/>
<TextBox Grid.Row="2" Margin="0,0,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
MinHeight="270" MinWidth="690"
x:Name = "gTBxInfo"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
</Window>
Remove MinHeight and MinWidth from the TextBox, and change HorizonalAlignment to Stretch
<TextBox Margin="0,0,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
x:Name = "gTBxInfo"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" />
Edit:
If you want to resize the TextBox in both directions (horizontally and vertically), you would have to use a different container other than StackPanel, so that the TextBox sizing is independent.
Something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Find" x:Name="gMNuFind" Grid.Row="0"/>
</Menu>
<Button x:Name="gBuFind"
Content=" Find "
Margin="5,10,5,5"
Grid.Row="1"/>
<TextBox x:Name = "gTBxInfo"
Margin="0,0,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
TextWrapping="Wrap"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="2"/>
</Grid>