WPF Textbox visible in Designer but not application - c#

I'm brand new WPF so I may be missing something obvious. I've got a TextBox in the designer, that looks fine and is visible, however, when running the app it isn't visible while the other elements are still visible.
Designer:
Application:
Xaml:
<Window x:Class="Windows.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:Windows"
mc:Ignorable="d"
Title="UniPaste for Windows" Height="450" Width="800"
Background="#333">
<Grid>
<TextBlock TextWrapping="Wrap" Text="UniPaste for Windows" Margin="0,0,0,348" TextAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="32" FontFamily="Consolas Bold"/>
<TextBlock TextWrapping="Wrap" Text="Email" Margin="0,90,0,0" TextAlignment="Center" VerticalAlignment="Top" Foreground="White" FontSize="24" FontFamily="Consolas"/>
<TextBox Name="Email" Margin="340,123,340,290" Background="White" Height="23" TextChanged="EmailChanged"/>
</Grid>
</Window>

Take advantage of the Grid component and its row and column definitions to align the elements you want.
What was happening is that at runtime the textbox had no line to attach to
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Background="#333">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="0 25 0 0"
TextWrapping="Wrap"
Text="UniPaste for Windows"
TextAlignment="Center"
VerticalAlignment="Center"
Foreground="White"
FontSize="32"
FontFamily="Consolas Bold"
/>
<TextBlock Grid.Row="1"
Margin="0 25 0 0"
TextWrapping="Wrap"
Text="Email"
TextAlignment="Center"
VerticalAlignment="Top"
Foreground="White"
FontSize="24"
FontFamily="Consolas"/>
<TextBox Grid.Row="2"
Name="Email"
Margin="0 25 0 0"
HorizontalAlignment="Center"
Background="White"
Height="23" Width="100"
TextChanged="EmailChanged"
/>
</Grid>
Runtime Window

Related

How to make window transparent when using Frames and Pages?

I have a simple application I'm trying to make.
The idea is to start with a login screen and then move to an admin screen. For this, after looking for answers I stumbled upon Frames and Pages. The problem that I'm having now is that it seems that Pages are not compatible with translucency?
This is what I get when using Frame and Page:
This is what I want to replicate:
This is the main window with pages:
<Window
x:Class="Membership2.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:Membership2"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1366"
AllowsTransparency="True" WindowStyle="None" Background="Transparent"
WindowStartupLocation="CenterScreen">
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
As you can see, Background property is set to Transparent, but it does nothing. I also tried setting the Grid Background and Frame Background to Transparent without success.
Also, WindowStartupLocation is set to CenterScreen when using Pages, but it also does nothing it seems.
EDIT:
So this problem arises when StartupUri (in App.xaml) is set to my LoginPage.xaml instead of the MainWindow.xaml (described previously).
This is my LoginPage.xaml:
<Page x:Class="Membership2.LoginPage"
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:Membership2"
mc:Ignorable="d"
d:DesignHeight="550" d:DesignWidth="450"
MaxHeight="550" MaxWidth="450"
Title="LoginPage"
RenderOptions.ClearTypeHint="Enabled"
FontFamily="./Resources/Fonts/#Dosis"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:fw="clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF"
ShowsNavigationUI="False"
Background="Transparent"
>
<Grid
fw:PointerTracker.Enabled="True" Background="Transparent">
<Border
Background="WhiteSmoke"
CornerRadius="7"
Margin="30, 75, 30, 0">
</Border>
<Ellipse
Width="50"
Height="50"
Fill="WhiteSmoke"
Margin="0,-450,0,0">
</Ellipse>
<materialDesign:PackIcon
Kind="AccountCircle"
Width="150"
Height="150"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Grid.RowSpan="2"
FontSize="50"
Foreground="#7ea4b3"
Grid.Column="1">
</materialDesign:PackIcon>
<Label
Content="Administración"
HorizontalAlignment="Center"
Margin="0,140,0,0"
FontSize="30"
FontFamily="./Resources/Fonts/#Dosis">
</Label>
<TextBox
x:Name="Username"
Width="230"
Height="45"
FontSize="25"
TextWrapping="NoWrap"
Style="{StaticResource MaterialDesignTextBox}"
FontWeight="Light"
RenderOptions.ClearTypeHint="Enabled"
FontFamily="./Resources/Fonts/#Dosis"
Margin="0, 0, 0, 50"
materialDesign:HintAssist.Hint="Usuario">
</TextBox>
<PasswordBox
x:Name="Password"
Width="230"
Height="45"
FontSize="25"
FontWeight="Light"
RenderOptions.ClearTypeHint="Enabled"
Margin="0, 150, 0, 0"
BorderThickness="0,0,0,1"
materialDesign:HintAssist.Hint="Contraseña"
materialDesign:TextFieldAssist.DecorationVisibility="Visible">
</PasswordBox>
<Button
x:Name="Login"
Width="150"
Height="48"
VerticalAlignment="Bottom"
Margin="0,0,0,70"
FontSize="25"
FontWeight="Light"
Background="LightGray"
Style="{StaticResource ButtonRevealStyle}"
BorderThickness="2,2,2,2">
Ingresar
</Button>
</Grid>
</Page>

Find a property of a sibling control

Windows 10 Pro 64, VS 2019 Community, WPF App
I have an app that has a UserControl with two members, each of which is a UserControl that contains a header which is 2 -3 lines of text, with a ListBox below it. My objective is to set the height of the one header to the height of the other, and I've chosen to bind the one to the other to accomplish that. But, it appears as if my RelativeSource is not resolving, which results in trying to set the height to a NaN, which, of course, does not change it. I've tried many different ways to express the RelativeSource, and none of them has worked. Here's the latest:
Here's the main window:
<Window x:Class="MyApp.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:MyApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:MyParentControl x:Name="Daddy"/>
</Grid>
</Window>
And the Parent control that contains the two children:
<UserControl x:Class="MyApp.MyParentControl"
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:MyApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<StackPanel Orientation="Horizontal">
<local:MySonControl x:Name="Sonny"/>
<local:MyDaughterControl x:Name="Girlie"/>
</StackPanel>
</Grid>
</UserControl>
The first child control:
<UserControl x:Class="MyApp.MySonControl"
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:MyApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical" x:Name="Header">
<TextBlock Text="Vertical space" HorizontalAlignment="Center"/>
<TextBlock Text="Sonny's Stuff" HorizontalAlignment="Center"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Column A" Margin="5,5,5,5"/>
<TextBlock Text="Column B" Margin="5,5,5,5"/>
<TextBlock Text="Column C" Margin="5,5,5,5"/>
</StackPanel>
</StackPanel>
<ListBox x:Name="Stuff"/>
</StackPanel>
</Grid>
</UserControl>
And the second, which contains the binding statement:
<UserControl x:Class="MyApp.MyDaughterControl"
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:MyApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical" x:Name="Header"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MyParentControl}, Path=Sonny.Header.ActualHeight}"
>
<TextBlock Text="Girlie's Stuff" HorizontalAlignment="Center"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Column A" Margin="5,5,5,5"/>
<TextBlock Text="Column B" Margin="5,5,5,5"/>
<TextBlock Text="Column C" Margin="5,5,5,5"/>
</StackPanel>
</StackPanel>
<ListBox x:Name="Stuff"/>
</StackPanel>
</Grid>
</UserControl>
I have not touched the code-behind, it's just as VS generated it.
Here's what it displays:
What we should see is that the right header is the same height as the left, but as you can tell, that is not the case.
I get the following error message in my output window, which I take to mean that it's not resolving the RelativeSource:
System.Windows.Data Error: 40 : BindingExpression path error: 'Sonny' property not found on 'object' ''MyParentControl' (Name='Daddy')'. BindingExpression:Path=Sonny.Header.ActualHeight; DataItem='MyParentControl' (Name='Daddy'); target element is 'StackPanel' (Name='Header'); target property is 'Height' (type 'Double')
Any ideas of what I'm doing wrong?
Use a SharedSizeGroup to sync the heights of the rows, and remove the StackPanels in favor of Grid rows:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" SharedSizeGroup="HeaderGroup"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Text="Girlie's Stuff" HorizontalAlignment="Center"/>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<TextBlock Text="Column A" Margin="5,5,5,5"/>
<TextBlock Text="Column B" Margin="5,5,5,5"/>
<TextBlock Text="Column C" Margin="5,5,5,5"/>
</StackPanel>
<ListBox Grid.Row="3" x:Name="Stuff"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" SharedSizeGroup="HeaderGroup"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Vertical space" HorizontalAlignment="Center"/>
<TextBlock Grid.Row="1" Text="Sonny's Stuff" HorizontalAlignment="Center"/>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<TextBlock Text="Column A" Margin="5,5,5,5"/>
<TextBlock Text="Column B" Margin="5,5,5,5"/>
<TextBlock Text="Column C" Margin="5,5,5,5"/>
</StackPanel>
<ListBox Grid.Row="3" x:Name="Stuff"/>
</Grid>
<Grid IsSharedSizeScope="True">
<StackPanel Orientation="Horizontal">
<wpfApp2:MySonControl x:Name="Sonny"/>
<wpfApp2:MyDaughterControl x:Name="Girlie"/>
</StackPanel>
</Grid>
Results in:
Depending on what the contents of the two child control will be, I strongly suggest to only use one child UserControl and use MVVM to populate it with different data. This will remove the need to maintain two basically identical controls.

How to make WPF Toolkit charts expand with to fit content?

I'm trying to create some bar charts from DotNetProjects.Wpf.Toolkit that display some data totals, though the data can change at run time. The problem is that I cannot get the chart to fit the size of the content - I always have to provide a fixed value (and setting Height="Auto" doesn't work), but I don't know what the size will need to be while running. What can I do to make my chart fit the content rather than the other way around?
My XAML code looks like this:
<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:Movie_Vault"
xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Layout.Toolkit" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="Movie_Vault.frmStatistics"
mc:Ignorable="d"
Title="frmStatistics" Style="{StaticResource RoundedFormStyle}" WindowStartupLocation="CenterOwner">
<Border Style="{StaticResource WindowBorderStyle}">
<DockPanel x:Name="OuterPanel" >
<Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Top">
<DockPanel x:Name="TopPanel" HorizontalAlignment="Stretch" VerticalAlignment="Top">
<Image Height="16" Margin="8,4,8,4" Source="Images/process.png"/>
<TextBlock Style="{StaticResource MediumFont}"><Run Text="Statistics"/></TextBlock>
<DockPanel DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center">
<Button x:Name="btnClose" Content="X" Style="{StaticResource MicroButtonStyle}" Margin="0,0,8,0" Click="btnClose_Click"/>
</DockPanel>
</DockPanel>
</Border>
<Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Left" Margin="0,8,0,0">
<DockPanel VerticalAlignment="Top" >
<ScrollViewer VerticalScrollBarVisibility="Auto" Height="400" Width="500">
<Grid Name="panelTopGenres">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<toolkit:Chart Name="chartTopGenres" Title="Top Genres" VerticalAlignment="Top"
Margin="16,16,16,8" Padding="8" Background="White" >
<toolkit:BarSeries DependentValuePath="Value" IndependentValuePath="Key"
ItemsSource="{Binding}"
IsSelectionEnabled="False" Background="White"/>
</toolkit:Chart>
<Button Grid.Row="1" x:Name="btnTopGenresToggle" Style="{StaticResource SmallButtonStyle}"
Content="Show All Genres" Width="120"
Margin="16,0,4,4" HorizontalAlignment="Left" Click="btnToggle_Click" />
</Grid>
</ScrollViewer>
</DockPanel>
</Border>
</DockPanel>
</Border>
</Window>
What can I do to make my chart fit the content rather than the other way around?
Get rid of any StackPanels:
<Grid Name="panelTopGenres">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<toolkit:Chart Name="chartTopGenres" Title="Top Genres" VerticalAlignment="Top"
Margin="16,16,16,8" Padding="8" Background="White" >
<toolkit:BarSeries DependentValuePath="Value" IndependentValuePath="Key"
ItemsSource="{Binding}"
IsSelectionEnabled="False" Background="White"/>
</toolkit:Chart>
<Button Grid.Row="1" x:Name="btnTopGenresToggle" Style="{StaticResource SmallButtonStyle}"
Content="Show All Genres" Width="120"
Margin="16,0,4,4" HorizontalAlignment="Left" Click="btnToggle_Click" />
</Grid>
A StackPanel doesn't resize its children.

C# Windiws IoT core - Display and Scroll text in Textblock over an Image Grid

Working on IoT Project using Windows IoT core. So Far Images, Videos and Audios are able to display on the Display. Need to add a scrolling Text at the bottom
The Code Developed so far XAML is:
<Page
x:Class="Digital_Notiec_Board_V1._2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Digital_Notiec_Board_V1._2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="#FF222222" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Image x:Name="imageInstance" Visibility="Collapsed" />
<MediaElement x:Name="audioInstance" Visibility="Collapsed" />
<MediaElement x:Name="videoInstance" Visibility="Collapsed" />
<TextBlock x:Name="ScrollText" HorizontalAlignment="Left" Height="63" Margin="0,1017,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="1910" FontSize="36" Foreground="White"/>
<!--
<WebView x:Name="webViewInstance" Visibility="Collapsed"/>
-->
</Grid>
Please help me out with suggestion. If possible any Base to check On.
Thanks in advance
You can use ScrollViewer to represent the text area.Please refer to following XAML codes.To make your app responsive and adaptive,it is a good choice to use XAML properties and layout panels.Please reference Define page layouts with XAML.
<Page
x:Class="Digital_Notiec_Board.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Digital_Notiec_Board"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="#FF222222" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition MaxHeight="200"></RowDefinition>
<RowDefinition MaxHeight="200"></RowDefinition>
<RowDefinition MaxHeight="200"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="imageInstance" Visibility="Collapsed" Grid.Row="0"/>
<MediaElement x:Name="audioInstance" Visibility="Collapsed" Grid.Row="1"/>
<MediaElement x:Name="videoInstance" Visibility="Collapsed" Grid.Row="2"/>
<ScrollViewer Grid.Row="3">
<TextBlock x:Name="ScrollText" TextWrapping="Wrap" Foreground="White" Text="TextBlock" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Auto">
</TextBlock>
</ScrollViewer>
</Grid>

How to correctly use a UserControl in WPF?

I created this user control
<UserControl x:Class="POS1.Windows.Controles.txttransparente"
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"
mc:Ignorable="d"
Height="auto" Width=" auto"
>
<Border BorderBrush="Yellow" Background="Transparent" CornerRadius="10,10,10,10" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
<TextBox Name="txt1" Background="Transparent" BorderBrush="Black" BorderThickness="3" Text="Usuario" FontSize="20" FontWeight="ExtraBold" ></TextBox>
</Border>
</UserControl>
When I add it to a Window,
<Controls:MetroWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Custom="http://modernwpf" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
x:Class="POS1.MainWindow"
xmlns:txt="clr-namespace:POS1.Windows.Controles"
Title="MainWindow" Height="292" Width="535" AllowsTransparency="True" WindowStyle="None"
>
<Grid>
<Grid.RowDefinitions >
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="133*"/>
<ColumnDefinition Width="134*"/>
<ColumnDefinition Width="135*"/>
<ColumnDefinition Width="133*"/>
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="4" Grid.RowSpan="7" CornerRadius="40,50,60,70">
<Border.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/b.jpg"/>
</Border.Background>
</Border>
<Border BorderBrush="Yellow" Background="Transparent" CornerRadius="10,10,10,10" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
<TextBox Background="Transparent" BorderBrush="Black" BorderThickness="3" Text="Usuario" FontSize="20" FontWeight="ExtraBold" ></TextBox>
</Border>
<TextBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Text="Contraseña" FontSize="20" FontWeight="ExtraBold" ></TextBox>
<Button Grid.Row="5" Grid.Column="1" Content="Aceptar" FontSize="12" FontWeight="ExtraBold" ></Button>
<Button Grid.Row="5" Grid.Column="2" Content="Olvidé contraseña" FontSize="12" FontWeight="ExtraBold" ></Button>
<txt:txttransparente Content=" Hola Mundo" Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2" ></txt:txttransparente>
</Grid>
</Controls:MetroWindow>
as you see I could not modify the txt1.Text, so I have instead used Content="hola mundo"
However this sees it as above, but not as similar to the button usuario.
Without explicitly navigating the control's visual tree, the elements in your UserControl need for you to implement mechanism to pass data from client code using your control to the elements contained within. A common strategy for doing this is to simply declare a property on your UserControl type itself, and then bind the appropriate member in the control's tree to that property.
For example:
class txttransparente : UserControl
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(txttransparente));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
Then in your XAML for the UserControl:
<UserControl x:Class="POS1.Windows.Controles.txttransparente"
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:POS1.Windows.Controles"
mc:Ignorable="d"
Height="auto" Width=" auto">
<Border BorderBrush="Yellow" Background="Transparent" CornerRadius="10,10,10,10"
Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
<TextBox Name="txt1" Background="Transparent" BorderBrush="Black"
BorderThickness="3"
Text="{Binding Text,
RelativeSource={RelativeSource AncestorType={x:Type local:
txttransparente}}}"
FontSize="20"
FontWeight="ExtraBold"/>
</Border>
</UserControl>
Note the change not only in the binding for the TextBox.Text property there, but also the addition of the xmlns:local declaration, so that the binding can find the parent UserControl object where the property exists.
These changes create the property, and connect it to the Text property of the control's TextBox element.
Then finally, where you use the UserControl, you simply set the Text property (instead of Content):
<txt:txttransparente Text="Hola Mundo" Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2"/>

Categories