Duplicating a control in wpf application-xaml - c#

I want to show a duplicate of a control which behaves same as source.In below mentioned code I have textbox1 that I want to show again.So I created one more text and binded its text with textbox1's text.If I change the text in textbox1 ,text changes in other tetxbox also.
But my problem is validation template is not getting applied on other control.How can I do that?
<TabItem Width="100" Height="50" Header="Tab1">
<AdornerDecorator>
<StackPanel>
<Label> First</Label>
<TextBox x:Name="TextBox1"
Margin="20" Height="50" Width="150"
Style="{StaticResource S_ErrorTemplate}"
Text="{Binding TestValue,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Margin="20" Height="50" Width="150"
Style="{StaticResource S_ErrorTemplate}"
Text="{Binding ElementName=TextBox1, Path=Text,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"
Template="{Binding ElementName=TextBox1,Path=Template}"/>
</StackPanel>
</AdornerDecorator>
</TabItem>
output is

Related

WPF bind list box Selected item into textbox

I'm trying to put any selected item's name in my list box in the textbox next to it. But I've got trouble doing so.
Here's a little bit of my code:
<DockPanel Margin="10">
<StackPanel Margin="5" DockPanel.Dock="Left">
<ListBox Name="lbNames" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel Margin="5" DockPanel.Dock="Right">
<TextBox Name="txtName" Width="auto" Text="{Binding ElementName=lbNames,Path=SelectedItem}"/>
<WrapPanel Margin="0,5" Orientation="Horizontal">
<Button Margin="2,0" Name="btnAdd" Click="btnAdd_Click" Content="Add"/>
<Button Margin="2,0" Name="btnEdit" Click="btnChange_Click" Content="Edit"/>
<Button Margin="2,0" Name="btnDelete" Click="btnDelete_Click" Content="Delete"/>
</WrapPanel>
</StackPanel>
</DockPanel>
Because you are showing Text="{Binding ElementName=lbNames,Path=SelectedItem}"and your selected item is an user object.
Instead of this you can use something like that.
First add SelectedValuePath="Name" into your listbox.
Then use Text="{Binding ElementName=lbNames,Path=SelectedValue}"
<DockPanel Margin="10">
<StackPanel Margin="5" DockPanel.Dock="Left">
<ListBox Name="lbNames" DisplayMemberPath="Name" SelectedValuePath="Name" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel Margin="5" DockPanel.Dock="Right">
<TextBox Name="txtName" Width="auto" Text="{Binding ElementName=lbNames,Path=SelectedValue}"/>
<WrapPanel Margin="0,5" Orientation="Horizontal">
<Button Margin="2,0" Name="btnAdd" Click="btnAdd_Click" Content="Add"/>
<Button Margin="2,0" Name="btnEdit" Click="btnChange_Click" Content="Edit"/>
<Button Margin="2,0" Name="btnDelete" Click="btnDelete_Click" Content="Delete"/>
</WrapPanel>
</StackPanel>
</DockPanel>
Or simply you can use it too Text="{Binding ElementName=lbNames,Path=SelectedItem.Name}"

How can I create stackpanels in wpf automatically without using xaml

Newbie here.
I'm writing a code to create information for different users. I want the stackpanels to be generated automatically.
For example if 5 user enters his/her details in the ui and click on the add button, 5 stackpanel with the users info,should be generated automatically for each users. Please what wpfcontrol can I use for this functionality? Thanks all.
<StackPanel >
<StackPanel Orientation="Horizontal" Height="32">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Margin="0,6,5,0"
Text="First Name" VerticalAlignment="Top"/>
<TextBox Name="firstname" TextWrapping="Wrap" Text="" Margin="0,0,0,9" Width="71"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="49" RenderTransformOrigin="0.502,1.031">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Margin="0,6,5,0"
Text="Last Name" VerticalAlignment="Top"/>
<TextBox Name="lastname" TextWrapping="Wrap" Text="" Margin="0,0,0,26" Width="71" TextChanged="TextBox_TextChanged"/>
</StackPanel>
</StackPanel>
<StackPanel VerticalAlignment="Bottom" Margin="10,10,0,0" FlowDirection="LeftToRight" Orientation="Horizontal" Grid.Row="1">
<Button x:Name="Btn_add" Content="Add" Click="Btn_add_Click"
HorizontalAlignment="Center" VerticalAlignment="Top" Width="44"/>
<StackPanel Margin="10">
<WrapPanel Margin="0,10">
<TextBlock Text="Full Name: " FontWeight="Bold" />
<TextBlock Text="{Binding Path=Text, ElementName=lastname}" />
<TextBlock Text="{Binding Path=Text, ElementName=firstname}" />
</WrapPanel>
</StackPanel>
</StackPanel>
You could create a UserControl that contains your StackPanel and of its content. You would then be able to instantiate that UserControl on-the-fly whenever you want it. See Creating & using a UserControl for a tutorial on WPF UserControl.

Obtain the value from textbox inside of a DataTemplate (WPF)

I´ve just created a datatemplate for a ListBox like that:
<ListBox Height="150" MinHeight="100" HorizontalAlignment="Left" Name="myListBox"
VerticalAlignment="Top" Width="290"
ItemsSource="{Binding}" SelectionMode="Multiple" Margin="0,18,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal">
<CheckBox Name="cbLista" Width="100" Content="{Binding Path=Nom_estudio}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="cbLista_Checked" />
<TextBox Name="txbCantidad" Width="100" Margin="0,0,0,5" TextChanged="txbCantidad_TextChanged" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And now I can get every object selected in the checkbox, but how can I obtain the text property for the texbox asociated for every checkbox ?
Bind the "Text" property of the TextBox to some property (say MyTextProperty) on your data object. Then when you get the "SelectedItems" list, you just access this property.
ie:
<TextBox Text="{Binding MyTextProperty}" ... />
Create on more property in your class which has Nom_estudio and IsChecked properties. Then bind that property to TextBox.Text property
<StackPanel Orientation="Horizontal">
<CheckBox Name="cbLista" Width="100" Content="{Binding Path=Nom_estudio}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="cbLista_Checked" />
<TextBox Name="txbCantidad" Text="{Binding MYTEXTPROPERTY}" Width="100" Margin="0,0,0,5" TextChanged="txbCantidad_TextChanged" />
</StackPanel>

How to get a control in a listbox on code behind windows phone?

I have a listbox in my windows phone application. In the listbox DataTemplate I placed a button. How can I get the button object in codebehind. I am not getting the reference of the rowButton in the .cs file. I want to change the button background color of button of each row. how can I get the button reference in the code behind ?
I following code I used for listview.
<Grid Height="530" Grid.Row="1" VerticalAlignment="Top" Margin="0,30,0,0">
<ListBox Margin="0,0,0,0" Name="TransactionList">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Width="460" Height="150" Click="user_click" Name="rowButton" >
<Button.Content>
<StackPanel Orientation="Horizontal" Height="auto" Width="400">
<Image Width="80" Height="80" Source="{Binding Type}"></Image>
<StackPanel Orientation="Vertical" Height="150" Margin="20,0,0,0">
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" FontSize="22" Text="Name :" Height="40" ></TextBlock>
<TextBlock Width="auto" FontSize="22" Text="{Binding Name}" Height="40" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" FontSize="22" Text="Date :" Height="40" ></TextBlock>
<TextBlock Width="100" FontSize="22" Text="{Binding Date}" Height="40" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" FontSize="22" Text="Amount :" Height="40" ></TextBlock>
<TextBlock Width="auto" FontSize="22" Text="{Binding Amount}" Height="40" ></TextBlock>
<TextBlock Width="auto" FontSize="22" Text=" $" Height="40" ></TextBlock>
</StackPanel>
</StackPanel>
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
If you want to change background on user click, in the click event handler use
Button button1 = sender as Button;
button1.Backgorund = new SolidColorBrush(Colors.Red);
to change the background color.
Else bind the background property for each button and change its value on iteration of the items in the listbox.
Without seeing how you've tried to access this is in code it's impossible to say why what you're doing isn't working.
However it'll be much easier if you use databinding to set the color of the templateitem

Items hidden when added to HyperLinkButton

The items below show up normally when I do not have them inside the HyperlinkButton.
However, when I add them to the HyperlinkButton, they become invisible.
<DataTemplate>
<HyperlinkButton NavigateUri="/ViewChallenge.aspx">
<HyperlinkButton.Content>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<Image Height="100" Width="100" Source="{Binding Path=Challenge.Image}" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding Path=Challenge.Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<HyperlinkButton NavigateUri="ViewUser.aspx" >
<HyperlinkButton.Content>
<TextBlock Text="{Binding Path=User.Username}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</HyperlinkButton.Content>
</HyperlinkButton>
</StackPanel>
</StackPanel>
</HyperlinkButton.Content>
</HyperlinkButton>
</DataTemplate>
As far as I know, the Hyperlink button only supports text. For example:
<HyperlinkButton Height="100" Width="300">
Hello World
</HyperlinkButton>
Maybe you should use a Button control and set a Control template and enter the XAML you mentioned above inside. It makes more sense in my opinion. Try this:
<ControlTemplate x:Key="MyButtonTemplate" TargetType="Button">
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<Image Height="100" Width="100" Source="{Binding Path=Challenge.Image}" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding Path=Challenge.Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Path=User.Username}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</ControlTemplate>
And simply set the template for your button like so:
<Button x:Name="myButton" Template="{StaticResource MyButtonTemplate}" Click="myButton_Click"/>
And then do the navigation inside the click event.

Categories