How to put textblock inside scrollviewer by coding behide ? (wpf) - c#

I have a xaml code like this
<Grid>
<... some grid row and column definitions .../>
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
<TextBlock "some attribute" />
</ScrollViewer>
</Grid>
I don't know how to tie Textblock with Scrollviewer in C#. I want to use Textblock with Scrollviewer.
If you have another idea, please tell me.
Many Thanks for your help. :D

just use the Content Property
var myScrollViewer = new ScrollViewer();
myScrollViewer.Content= new TextBlock();
Edit
or in combination with XAML
XAML
<ScrollViewer Name="myScrollViewer " Grid.Column="1" VerticalScrollBarVisibility="Auto"/>
Code behind
myScrollViewer.Content= new TextBlock(); // or what ever you want to add :-)

Related

Controls overlapping tabitem wpf c#

I have a TabControl,inside one Tabitem, i have a grid and my userControl inside the grid:
<TabControl>
<TabItem>
<Grid HorizontalAlignment="Left" Height="64" Margin="288,150,0,0" VerticalAlignment="Top" Width="354">
<Canvas>
<local:MyCustomComboBox x:Name="ucc1" HorizontalAlignment="Left" Grid.RowSpan="2" Grid.ColumnSpan="3" Height="30" VerticalAlignment="Top" Width="194" ClipToBounds="True"/>
<Canvas>
</Grid>
<TabItem>
<TabControl>
By default,when the userControl's size is greater than the grid's/TabItem's size,the extra portion can't be seen.How can i make my UserControl overlap it ? I tried to add RowSpan and ColumnSpan but it didn't work :(
TabItem has it's own bounds which u cannot overlap.So,there's no way u can achieve your goal ... But i always try my best to help people, so here's a quick tip :
If the usercontrol is bigger than the gird
Your userControl XAML includes MyCustomComboBox, which makes me think it is a combobox..I've seen your previous post where you wanted to customize your combobox but couldn't quite achieve your goals...So, are you trying to create you own custom combobox and by usercontrol bigger than the grid , did u mean that the drop down menu u created doesn't go outside the grid rather is clipped to the grid ??
If this is the case , u can use a ContextMenu and move your custom drop-down list there.Then the contextMenu will overlap both the TabItem and Grid as it is a window itself.
Also , NOTE that u cannot use Named content in usercontrol(u can but that requires a workaround).So i suggest you to add all required code behinds , even set required binding from the user-control's code behind.
Hope this helps :)

Create an Element after a specific Element in a StackPanel

I need to create a XAML textbox immediately after another textbox using code-behind. Maybe something like this:
Before
<StackPanel>
<TextBox Name="TextBox1"/>
<TextBox Name="TextBox2"/>
<TextBox Name="TextBox3"/>
</StackPanel>
After
<StackPanel>
<TextBox Name="TextBox1"/>
<TextBox Name="TextBox2"/>
<TextBox Name="InsertedTextBox"/> <!--This textbox was inserted after 'TextBox2'-->
<TextBox Name="TextBox3"/>
</StackPanel>
I have the name of the textbox I wish to insert the other textbox after. May I know how I can insert a textbox after another textbox which I know the name of?
Note: I am programming for Universal Windows.
Thanks in advance.
You need to name the StackPanel to reference it in code, and you need the index of the preceding TextBox:
var index = this.stackPanel1.Children.IndexOf(TextBox2);
this.stackPanel1.Children.Insert(index + 1, new TextBox { Name = "InsertedTextBox" });
You could try this, note I've given the StackPanel a name.
// Where 2 is the index.
this.StackPanel1.Children.Insert(2, new TextBox());

how to maintain the Width of tabItems should be equal in WPF?

how to maintain the width of the tabItems should be equal if i open multiple tabItems. know i am not getting this thing. i tried the following code
HorizontalContentAlignment="stretch"
i used the above code but it is not working. but i am getting like this
for tabcontrol i write the following code
<TabControl Name="tabControl1" Margin="175,44,0,0" >
<Grid></Grid>
</TabControl>
tabItems will dynamically adding to the Tabcontrol
TabItem tabitem2 = new TabItem();
Page2 pgobj1 = new Page2();
Frame tabframe1 = new Frame();
tabframe1.Content = pgobj1;
tabitem2.Header = "Tab 2Tab 2Tab 3";
tabitem2.Width = 300;
tabitem2.Content = tabframe1;
tabControl1.Items.Add(tabitem2);
tabitem2.IsSelected = true;
i don't want those spaces in between the tabItems. and they automatically resize if more number of tabs are opened. kindly help me out in this.
After removing the Hardcoded width property
it comes like this
Remove hardcoded width you are setting on TabItem. No need to set that, TabItem will automatically adjust its width base on header content.
tabitem2.Width = 300; // Remove this line
If you want items to come in single line and scroll using ScrollViewer, you need to modify Template of TabControl like mentioned here. I am posting the code from there for the sake of completeness of answer:
<TabControl Name="tabControl1">
<TabControl.Template>
<ControlTemplate TargetType="TabControl">
<StackPanel>
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled">
<TabPanel x:Name="HeaderPanel"
Panel.ZIndex ="1"
KeyboardNavigation.TabIndex="1"
Grid.Column="0"
Grid.Row="0"
Margin="2,2,2,0"
IsItemsHost="true"/>
</ScrollViewer>
<ContentPresenter x:Name="PART_SelectedContentHost"
SnapsToDevicePixels="{TemplateBinding
SnapsToDevicePixels}"
Margin="{TemplateBinding Padding}"
ContentSource="SelectedContent"/>
</StackPanel>
</ControlTemplate>
</TabControl.Template>
</TabControl>
Also check out this answer here.
If I understand you correctly, you want that all tab items should have equal width. You can achieve that by wrapping each tab item header inside a Grid with a ColumnDefinition that uses a SharedSizeGroup. Furthermore, you must mark the TabControl like this:
<TabControl x:Name="tabControl1" Grid.IsSharedSizeScope="True">
The following code shows how to set the Header property programmatically:
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition
{
SharedSizeGroup = "MySharedSizedGroupName",
Width = new GridLength(1.0, GridUnitType.Auto)
});
grid.Children.Add(new TextBlock{Text="Tab 5"});
tabitem2.Header = grid;
EDIT: And one other point: I think the <Grid></Grid> you have added as a child to the TabControl is unnecessary, because it creates that empty tab on the left side in the first tab row.

remove a element from a StackPanel and add it to other StackPanel

is it possible to remove a item e.g. a TextBlock from a StackPanel and add it to a other StackPanel?
My XAML Code looks like this:
<StackPanel x:Name="PanelStack1" VerticalAlignment="Bottom" Margin="0,0,0,86">
<Rectangle x:Name="RecX" />
</StackPanel>
<StackPanel x:Name="PanelStack2" VerticalAlignment="Bottom" Margin="0,0,0,86" />
I want to move the RecX from the PanelStack1 to PanelStack2 via my C# Code. Is this possible?
This is just a small example, in my real code there are many elements which have to been moved.
Try this in your code behind:
PanelStack1.Children.Remove(RecX);
PanelStack2.Children.Add(RecX);

Expand row in WPF datagrid

I am using a DataGrid to display some log files, where each cell contains a TextBlock. I need help creating a method to expand a user selected row like this:
This is my code right now. It is based on the index of the clicked row:
DataGridRow testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
logBrowserDataGrid.UpdateLayout();
logBrowserDataGrid.ScrollIntoView(logBrowserDataGrid.Items[index]);
testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
testrow.Height = 100;
However this creates a weird result:
Do you know a god way to expand a row based on the index?
Do you know what happens in the weird result i get? It looks like i am expanding a part of the row, and the rest does stretch out. I have also studied it in runtime, and can see that its height is the correct 100, but the ActuallyHeight is still 20.
Additional info:
The default size of the rows are set by the .RowHeight property on the DataGrid.
I am using the AutoGenerateColumns feature, plus catching the AutogeneratingColumn event to replace the column with a DataGridTemplateColumn.
Why not replace the default DataGridCellTemplate with an Expander to do all that for you?
<DataGridColumn>
<DataGridColumn.CellTemplate>
<DataTemplate>
<Expander Header="{Binding SomeText}">
<TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
</Expander>
</DataTemplate>
</DataGridColumn.CellTemplate>
</DataGridColum>
If you don't like the default Expander look, you can overwrite it's Template to look like a plain TextBlock
As a side note, to stretch and vertically align a DataGridRow, you want to stretch and align the Cell Content, not the Row.
I tried with an Expander and the functionality was precisely what I wanted, but the look was not however. I have tried restyling the Expander to fit my need, but gave up because of the events I needed to add to it (XamlReader + events was more than my programming skills could handle). But based on Rachels suggestion I made a UserControl with the following content:
<StackPanel Orientation="Vertical" MouseUp="StackPanel_MouseUp">
<TextBlock Name="headerTextBlock" Margin="3,2,3,2" Height="20" Width="Auto" TextWrapping="NoWrap"/>
<TextBlock Name="textTextBlock" Margin="3,2,3,2" Height="Auto" Width="Auto" TextWrapping="NoWrap" Visibility="Collapsed"/>
</StackPanel>
In the codebehind I then can handle the event "StackPanel_MouseUp" to change the visibility of the TextBlocks. This control looks and works like I wanted the restyled Expander to do.
Now my xaml string looks like this.
string xamlCellTemplateFormat =
#"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
~local~>
<local:CustomExpander x:Name=""UserControlTest"" Header=""{Binding Path=~binding~}"" Text=""{Binding Path=~binding~}""/>
</DataTemplate>";
string xamlCellTemplate = xamlCellTemplateFormat.Replace("~binding~", e.Column.Header.ToString());
xamlCellTemplate = xamlCellTemplate.Replace("~local~", " xmlns:local=\"clr-namespace:IS.AppFramework.Windows.LogBrowserWPF;assembly=" + Assembly.GetExecutingAssembly().GetName().Name + "\"");

Categories

Resources