codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Update ImageSource when button clicked


By : , Category : c#

You can try this.

If your silverlight app is under /ClientBin folder, the url should be relative to the ClientBin. So if your image locates directly under your web app, you can try new Uri("../path.png", UriKind.Relative).

You can also use fiddler or chrome developer tools (Network tab) to capture your image request, which will show you the url you are requesting for.

ReLated :

Because you are not executing your update command. Just use ExecuteNonQuery like;

cmdUpdate.ExecuteNonQuery();

But more important, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Also use using statement to dispose your connection, command and reader automatically instead of calling Close or Dispose methods manually.

As I understand you want to change the Background one time after user pressed the Button? If so you can use style with animations:

<Window x:Class="ButtonBackground.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Black"/>
            <Setter Property="FontSize" Value="100"/>
            <Setter Property="Foreground" Value="White"/>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Background)" >
                                <ObjectAnimationUsingKeyFrames.KeyFrames>
                                    <DiscreteObjectKeyFrame>
                                        <DiscreteObjectKeyFrame.Value>
                                            <SolidColorBrush Color="Black"/>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                    <DiscreteObjectKeyFrame>
                                        <DiscreteObjectKeyFrame.Value>
                                            <!--<SolidColorBrush Color="Pink"/>-->
                                            <ImageBrush ImageSource="{Binding MyImgSource}" />
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames.KeyFrames>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button Grid.Row="0" Grid.Column="0" Content="1" Style="{StaticResource ButtonStyle}" />
    </Grid>
</Window>

If you want to return Black background to the Button after pressing then your style will be:

        <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Black"/>
            <Setter Property="FontSize" Value="100"/>
            <Setter Property="Foreground" Value="White"/>
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <!--<SolidColorBrush Color="Pink"/>-->
                            <ImageBrush ImageSource="{Binding MyImgSource}" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).

It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.

Button.Image will accept images stored as resources in your platform projects, and loaded either with:

Icon.Image = ImageSource.FromFile ("foobar.png");

or

Icon.Image = "foobar.png";

One thing you must understand is you shouldn't ask for approach to a problem. However since your question requires it.

First you should add Tags to the buttons to differentiate them:

button1.Tag = "1";
button2.Tag = "2";
.
.
.

Then in your event handler cast the sender object to your control type:

Button a = (button)sender;

Then check the tag of the button a to add frequencies:

   freqArray[Convert.ToString(a.Tag)-48] = freqArray[Convert.ToString(a.Tag)-48] + 1;
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .