how to Change "source" of a user defined style Programmatically
I am very new to .Net, C# and WP7 but working on same. I need to create an array of 开发者_StackOverflow"buttons" from code behind(C#). Each button have a textblock and a picture but text of Textblock and source of picture will be diff. for rach button.So i had created a Style-style having a textblock and an Image. Now need to set text to TextBlock and imageSource to Image at the time creating the button. Please help me out with sample code as i am trying for this since last 3 days but can't find the solution or mya be missing with some basic technique.
<phone:PhoneApplicationPage.Resources>
<Style x:Name="style" x:Key="UnChecked" TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" >
<Canvas x:Name="list" >
<TextBlock x:Name="Site" **Text=""** TextWrapping="NoWrap" Canvas.Top="104" Width="114" Height="48" Foreground="White" FontWeight="Bold" FontSize="24" VerticalAlignment="Top" CacheMode="BitmapCache" Canvas.Left="16" />
<Image x:Name="Logo" Width="128" Height="104" UseLayoutRounding="True" **Source=""** />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
//Code behind
Please help me out OR let me know if me doing some blunder
There are multiple ways, as usual ... one thing you could do, depending on your needs is. You create a class that contains your informations like
public class ButtonInfo
{
public string Text{get;set;}
public BitmapSource Image {get;set;}
public ICommand Command{get;set;}
}
now, when you create your buttons
var btn = new Button();
vat info = new ButtonInfo();
info.Text ="Some caption";
// load an image
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");
myBitmapImage.EndInit();
// set the image
info.Image = myBitmapImage;
// set the button info onto our button
btn.DataContext = info;
Now you need to modify you style a bit
<Canvas x:Name="list" >
<TextBlock x:Name="Site" Text="{Binding Text}" />
<Image x:Name="Logo" Width="128" Height="104" Source="{Binding Image}" />
</Canvas>
So what did we do here? We created an object holding the dynamic informations for our buttons into ButtonInfo
, on creation of the Buttons we set these individual ButtonInfos onto the Buttons, now via DataBinding we got the Data out of the ButtonInfo into our Controls.
I added the Command property aswell because i think this would be the next thing you could stumple apon. Which can be fixed, adding
<Setter Property="Command" Value="{Binding Command}"/>
to your button style.
Well like i said, this is just one way to do it and imo not the best way. But it might be sufficient.
Also i need to add that i come from a WPF background and some things might be different in silverlight, but i guess these fundamental concepts are the same.
Hope that helps.
精彩评论