ToggleButton EventTriggerBehavior-将图像绑定到togglebutton的背景

时间:2017-03-21 14:27:06

标签: xaml uwp win-universal-app behavior eventtrigger

我的xaml中有以下代码:

<ToggleButton x:Name="play"    Command="{Binding OnPlayButton}" CommandParameter="{Binding ElementName=media , Mode=TwoWay}"  HorizontalAlignment="Left" Margin="573,638,0,0" VerticalAlignment="Top" Height="50" Width="50">
    <Image Name="MyImage"  Source="Images/Control/Play.png"/>
    <interactivity:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="Checked">
            <core:ChangePropertyAction  PropertyName="MyImage" Value="{Binding Source=Images/Control/Pause.png}" />
        </core:EventTriggerBehavior>
        <core:EventTriggerBehavior EventName="Unchecked">
            <core:ChangePropertyAction   PropertyName="MyImage" Value="{Binding Source=Images/Control/Play.png}" />
        </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</ToggleButton>

我想要实现的是在点击时将togglebutton的背景更改为pause.png,再次单击时将其更改为play.png。我在xaml中遇到异常,这是正确的方法吗?

2 个答案:

答案 0 :(得分:1)

你几乎有行为。但PropertyName不是Image它是SourceTargetObjectMyImage

<ToggleButton HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="True"> <Interactivity:Interaction.Behaviors> <Core:EventTriggerBehavior EventName="Checked"> <Core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Source" Value="Images/Control/Pause.png" /> </Core:EventTriggerBehavior> <Core:EventTriggerBehavior EventName="Unchecked"> <Core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Source" Value="Images/Control/Play.png"/> </Core:EventTriggerBehavior> </Interactivity:Interaction.Behaviors> <Image x:Name="MyImage" Source="Images/Control/Play.png" Width="100" Height="100"/> </ToggleButton>

您的代码应如下所示。

// In my application.conf
// default data source
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/myDb?useSSL=false"
db.default.username=myuser
db.default.password="mypassword"

// additional data source
db.anothersource.driver=com.mysql.jdbc.Driver
db.anothersource.url="jdbc:mysql://localhost:3306/myothersource?useSSL=false"
db.anothersource.username=myuser
db.anothersource.password="mypassword"

// Then in Java, I create a JooqContextProvider class to expose both connections.
public class JooqContextProvider {

    @Inject
    Database db;

    @Inject
    play.Configuration config;

    public JooqContextProvider(){}

    /**
     * Creates a default database connection for data access.
     * @return DSLConext.
     */
    public DSLContext dsl() {
        return DSL.using(new JooqConnectionProvider(db), SQLDialect.MYSQL);
    }

    /**
     * Creates an anothersource database connection for data access.
     * @return DSLConext for anothersource.
     */
    public DSLContext anotherDsl() {
        return DSL.using(
            new JooqAnotherSourceConnectionProvider(
                config.getString("db.anothersource.url"),
                config.getString("db.anothersource.username"),
                config.getString("db.anothersource.password")),
            SQLDialect.MYSQL);
    }
}

// Then I needed to implement my JooqAnotherSourceConnectionProvider

public class JooqAnotherSourceConnectionProvider implements ConnectionProvider {

    private Connection connection = null;
    String url;
    String username;
    String password;

    public JooqAnotherSourceConnectionProvider(String url, String username, String password){
        this.url = url;
        this.username = username;
        this.password = password;
    }

    @Override
    public Connection acquire() throws DataAccessException {
        try {
            connection = DriverManager.getConnection(url, username, password);
            return connection;
        }
        catch (java.sql.SQLException ex) {
            throw new DataAccessException("Error getting connection from data source", ex);
        }
    }

    @Override
    public void release(Connection releasedConnection) throws DataAccessException {
        if (connection != releasedConnection) {
            throw new IllegalArgumentException("Expected " + connection + " but got " + releasedConnection);
        }
        try {
            connection.close();
            connection = null;
        }
        catch (SQLException e) {
            throw new DataAccessException("Error closing connection " + connection, e);
        }
    }
}

// Then in Java code where I need to access one or the other data sources...
jooq.dsl().select().from().where()...
jooq.anotherDsl().select().from().where()...

祝你好运。

答案 1 :(得分:0)

您无法使用PropertyName="MyImage",因为“MyImage”是控件的名称,而不是ToggleButton的属性!

执行所需操作的最简单方法是依次添加两个图像,并根据控件状态更改可见性:

<ToggleButton x:Name="play">
    <Image Source="Images/Control/Play.png" Visibility="{Binding IsChecked, ElementName=play, Converter={StaticResource BooleanToVisibilityConverter}" />
    <Image Source="Images/Control/Stop.png" Visibility="{Binding IsChecked, ElementName=play, Converter={StaticResource InverterBooleanToVisibilityConverter}" />
</ToggleButton>

在上面的示例中,我使用两个转换器实例来将bool值转换为Visibility值。您可以自己编写,也可以像Cimbalino Toolkit中的BooleanToVisibilityConverter一样使用第三方。