WPF Set DataTemplate Grid size in code behind (ResourceDictionary) - c#

I have ResourceDictionary that holds Dialog template. It has own DataType="{x:Type viewModels:DialogViewModel}". I would like to set this dialog "window" size on initialization. I know how to do it if I will add for example Height property to DialogViewModel. However this is not the right place to specify Height. How to do it in code behind and then Bind to that property? I think I have tried all the possible solutions I was able to find.
Basically I need to specify Height in SplitDialog.xaml.cs, let's say Height=500 and then add it to <Grid Margin="20,20,20,10" Tag="Category dialog" MinHeight="450" Height="???" x:Name="MainGrid">, but how?
I have tried to add to SplitDialog.xaml.cs (returns Height is null):
Grid gGrid = (Grid)this.FindName("MainGrid");
gGrid.Height = 600;
SplitDialog.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Controls.Styles.Dialog.SplitDialog"
xmlns:viewModels="clr-namespace:ViewModels.Category;assembly=ViewModels">
<DataTemplate DataType="{x:Type viewModels:DialogViewModel}">
<DataTemplate.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</ResourceDictionary>
</DataTemplate.Resources>
<Grid Margin="20,20,20,10" Tag="Category dialog" MinHeight="450" x:Name="MainGrid">
</Grid>
</DataTemplate>
</ResourceDictionary>
SplitDialog.xaml.cs:
public partial class SplitDialog : ResourceDictionary
{
public SplitDialog()
{
}
}

Provide this logic in Loaded event of the Grid, e.g:
<Grid Loaded="Grid_Loaded">...
then in code-behind:
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
Grid grid = sender as Grid;
if (grid != null)
{
grid.Height = 600;
}
}
no?

Related

How to add some of my code to xaml property ResizeMode= CanResizeWithGrip or whenever it is called

Hi at the begining of this question i want to say that i'm beginer and thing i've done here could prolly be done better but i'm still learnig. i want to set a value of some variable that contains the actual width of grid, and whenever i resize my window obviously this width change and I don't know how to access it. What i want to do ? Just want my progress bar to scale up or down when resizing
Here is some code
<Window x:Class="Lista6.MainPage"
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:Lista6"
mc:Ignorable="d"
Title="MainPage" Height="650" Width="1000"
Background="#36393F"
ResizeMode="CanResizeWithGrip"
Icon="/img/Ikonka2.png"
WindowStyle="None">
///other xaml code
<ProgressBar Foreground="White"
Value="50"
BorderThickness="0"
Background="Gray"
Height="5"
Width="{Binding Path=ProgresBarWidth[0], Mode=OneWay}"/>
</StackPanel>
</Grid>
</Grid>
</Window>
ObservableCollection<double> width = new ObservableCollection<double> { };
public ObservableCollection<double> ProgresBarWidth
{
get
{
return width;
}
set
{
width = value;
}
}
public AppOperator()
{
width.Add(0);
}
private void ProgresbarWidthSetter()
{
MainPage mp = new MainPage();
width[0] = mp.MusicBar.Width;
}
Here as u can see i ve binded progres bar width to ObservColl property, and i want to call it when im resizing my window but i ve no idea how to do that.
Thanks for any help and wish u all good.

Set Content of an Element without a Name-Property

I'm using a ListBox in combination with a ObservableCollection. The content is set via a TemplateSelector (TextBlock or Label). The text has to be formatted (f.e. with Run-Tags in Code-behind), but i can't access the Items. Is there a solution to get the elements?
I've tried the usage of OfType<>, but this works only on Panels. I searched for an children-attribute but, there isn't one for ListBoxes. Setting the Name-Property via binding is not possible for UId and Name.
An IEnumerator for the LogicalChildren doesn't work and iterate over the whole content everytime a new element is added, is not so optimal. Here a minimal example.
<Window.Resources>
<DataTemplate x:Key="TextBlockTemplate">
<StackPanel>
<TextBlock />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="LabelTemplate">
<StackPanel>
<Label/>
</StackPanel>
</DataTemplate>
<local:myTemplateSelector x:Key="myTemplateSelector" x:Name="myTemplateSelector" TextBlockTemplate="{StaticResource TextBlockTemplate}" LabelTemplate="{StaticResource LabelTemplate}"/>
</Window.Resources>
<Grid Margin="0">
<ListBox Name="mylist" Grid.Row="3"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding _listEntries}"
ItemTemplateSelector="{StaticResource myTemplateSelector}"
>
</ListBox>
</Grid>
Greetings and thanks :)
The TextBlock has an Inlines property that returns the Inline elements that comprise the contents of the TextBlock.
The Label has a Content property that you, depending on how you are using it, may cast to a Panel.
There are no inline elements for a TextBox.
Now, I found a solution. I made the TextBlock and Label as User Control and set the Name-property. In the code-behind, I have access to the DataContext and the element can set itself.
<UserControl x:Class="Test.TextBlockControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TextBlockControl"
Loaded="UserControl_Loaded">
<Grid>
<StackPanel HorizontalAlignment="Stretch" Margin="0,0,0,0">
<TextBlock Name="textBlock"/>
</StackPanel>
</Grid>
In the Code behind i can now access the values and set:
public partial class TextBlockControl : UserControl
{
public List<string> name => DataContext as List<string>;
public TextBlockControl()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
foreach (var t in name)
{
var run = new Run(t.Text);
if (t.IsHighlighted)
{
run.Foreground = Brushes.Green;
}
else
{
run.Foreground = Brushes.Red;
}
textBlock.Inlines.Add(run);
}
}
}
}
In the MainWindow, the dataTemplate then references the UserControl (root is the namespace):
<root:PickControl />

XamDataGrid header Layout

I'm using the XamDatagrid v14.2 and want to get a specific layout showing when the grid is loaded.
There are three fields being displayed in the grid: RowNumber, WebHeaderText, and WebText. I want to display the grid so the WebText column is showing right under WebHeaderText.
I was able to get it working by manually dragging the WebText column right below WebHeaderText. How do I do that programmatically so the WPF application starts up like that?
The before image below is currently how it looks when I load the WPF app. I'm trying to get the "After" layout programmatically.
Before:
After:
Here is the XAML code:
<window 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:ViperWebUIAnnouncement" xmlns:custom="http://infragistics.com/DataPresenter" x:class="ViperWebUIAnnouncement.MainWindow" xmlns:igdp="http://infragistics.com/DataPresenter" mc:ignorable="d" title="Web Announcements">
<grid name="layoutRoot" horizontalalignment="Left">
<igdp:xamdatagrid name="xamDataGrid1" height="800" verticalalignment="Top">
<igdp:xamdatagrid.fieldlayoutsettings>
<igdp:fieldlayoutsettings autogeneratefields="False" allowaddnew="True" allowdelete="True" addnewrecordlocation="OnTop"></igdp:fieldlayoutsettings>
</igdp:xamdatagrid.fieldlayoutsettings>
<igdp:xamdatagrid.fieldlayouts>
<igdp:fieldlayout>
<igdp:fieldlayout.fields>
<igdp:field name="RowNumber" width="InitialAuto"></igdp:field>
<igdp:field name="WebHeaderText" width="InitialAuto"></igdp:field>
<igdp:field name="WebText" width="InitialAuto"></igdp:field>
</igdp:fieldlayout.fields>
</igdp:fieldlayout>
</igdp:xamdatagrid.fieldlayouts>
</igdp:xamdatagrid>
</grid>
</window>
Here is the C# code:
WebAnnouncementData webAnnouncementData;
public MainWindow()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
//XamDataGrid xamDataGrid1 = new XamDataGrid();
webAnnouncementData = new WebAnnouncementData(Environment.UserName);
BindingList<WebAnnouncement> webAnnoucementList = webAnnouncementData.GetWebAnnouncements();
xamDataGrid1.DataSource = webAnnoucementList;
}
I was able to get the layout I wanted in the grid by having WebHeaderText and WebText in the same column and on different rows.
Here is the code:
<igDP:Field Name="WebHeaderText" Width="InitialAuto" Row="0" Column="1"/>
<igDP:Field Name="WebText" Width="InitialAuto" Row="1" Column="1"/>

WPF Clear UserControl's children

I'm creating a UserControl in WPF, and the UserControl works so that when the user moves mouse over the control, it's childcontrols should be removed, but I don't seem to find the Children property or anything like that..
XAML is here:
<UserControl x:Class="myTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Name="thisTextBox"
mc:Ignorable="d" d:DesignWidth="300" Height="57" MouseEnter="UserControl_MouseEnter_1" MouseLeave="UserControl_MouseLeave_1">
<TextBlock Name="TypeText" TextWrapping="NoWrap" Text="" />
</UserControl>
And in code I need to do something like this to get the TextBlock go away:
private void UserControl_MouseEnter_1(object sender, MouseEventArgs e)
{
Children.Clear(); // There is no such thing as children here!!!
}
The "child element" of the UserControl is contained in the Content property. You can set it to null in order to remove the content.
private void UserControl_MouseEnter_1(object sender, MouseEventArgs e)
{
Content = null;
}

How to add scroll in a textBox in windows 8 app?

I want to add a scroll viewer in a textBOX in a windows 8 app. So that user can scroll through his long text
Add a scrollviewer into the XAML. Then set the margins of the scrollviewer by cutting and pasting that of the textbox, and set the height and width of textbox to auto.
I just rewrote what Harshit explained and added some pics and Code.
Add a scrollviewer to your XAML
Select Margin in Scrollviewer (Best way: Click on the TestViewer-Code in the XAML to select it)
Set the Margin Values to Auto
Paste (in XAML) the Textbox and paste it in the Scrollviewer-Tag
Set the Height and width of Textbox to Auto
done!
XAML:
<Page
x:Class="testapp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:newcalapp_winrt"
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}" Margin="0,4,0,-4">
<Button Click="showText" Content="ShowText" x:Name="btn" Width="200" Height="56" Margin="1037,620,0,92"></Button>
<ScrollViewer x:Name="outputTextBoxScrollViewer" Margin="57,200,700,400">
<TextBox x:Name="outputTextBox" AcceptsReturn="True"/>
</ScrollViewer>
<ScrollViewer x:Name="outputTextBlockScrollViewer" Margin="57,450,700,169">
<TextBlock x:Name="outputTextBlock"/>
</ScrollViewer>
</Grid>
</Page>
C#:
namespace testapp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
void showText(object sender, RoutedEventArgs args)
{
//OutputString
String outputString;
//Random number
Random randomizer = new Random();
int randomNumber = randomizer.Next(0,100000);
//Some magic with Dates :) Not important!
...
outputTextBox.Text = outputString;
outputTextBlock.Text = outputString;
}
}
}
http://i.stack.imgur.com/2qDyJ.png">

Categories