如何在UWP应用的调用弹出窗口中将Buttons内容作为属性获取?

时间:2019-05-10 19:21:19

标签: c# mvvm uwp uwp-xaml

用例

我有一堆(8)按钮,可通过set style属性打开一个天桥。

天桥包含一个“应用”按钮。该按钮将触发一个Command,该命令将在视图模型中执行与源按钮的内容(1到8)相关的操作。

问题

如何获取触发“天桥”到我的命令的按钮的内容值?

我认为CommandParameter={Text ElementName=Source}应该做我想做的事,但不应该。

资源

<Style x:Key="PixelButtonStyle" TargetType="Button">
    <Setter Property="Width" Value="50" />
    <Setter Property="Height" Value="50" />
    <Setter Property="Margin" Value="5" />
    <Setter Property="Flyout" Value="{StaticResource PixelColorPickerFlyOut}" />
</Style>

<Flyout x:Key="PixelColorPickerFlyOut">
            <StackPanel>
                <!-- Picker -->
                <ColorPicker x:Name="PixelColorPicker"
                             IsAlphaEnabled="False"
                             IsColorChannelTextInputVisible="False"
                             IsAlphaSliderVisible="False"
                             IsAlphaTextInputVisible="False"
                             IsHexInputVisible="False"
                             IsColorSliderVisible="False"
                             Width="300"/>

                <!-- Button row -->
                <StackPanel Orientation="Horizontal" 
                            Margin="0 20 0 0">
                    <Button Content="Apply"
                            Width="100"
                            Margin="0 0 10 0"
                            Command="{Binding ApplyColorCommand}"/>

                    <Button Content="Cancel"
                            Width="100"/>

                    <TextBlock Text="{Binding Path=Source}" />
                </StackPanel>
            </StackPanel>
        </Flyout>

用法

<Button Style="{StaticResource PixelButtonStyle}" Content="" />

更新1

我还试图用作弹出按钮:

<Button Command="{Binding ElementName=Grid, Path=DataContext.ApplyColorPickerCommand}" 
        Content="Apply" />

和触发弹出按钮的按钮:

<Button Style="{StaticResource PixelButtonStyle}" 
        DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}" 
        Content="1" 
        Background="Green"/>

但没有积极作用。

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解您的要求。您实际上是这个意思吗,您有8个按钮,每个按钮都可以引发弹出按钮。因此,您想知道哪个按钮实际上引发了弹出按钮吗?如果这是您想要的,也许您可​​以尝试以下代码:

首先,每个FramewrokElement都有一个称为tag的属性。这是一个演示XAML:

 <Grid>
    <Button Tag="button no.1" Command="{Binding MyBtnClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=Tag}" Content="set empty content"/>
</Grid>

然后,如果您使用的是RelayCommand,我们可以从此类中获取标记值:

 public class RelayCommand : ICommand
{
    private readonly Action _execute;
    private readonly Func<bool> _canExecute;
    /// <summary>
    /// Raised when RaiseCanExecuteChanged is called.
    /// </summary>
    public event EventHandler CanExecuteChanged;
    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action execute)
        : this(execute, null)
    {
    }
    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action execute, Func<bool> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }
    /// <summary>
    /// Determines whether this RelayCommand can execute in its current state.
    /// </summary>
    /// <param name="parameter">
    /// Data used by the command. If the command does not require data to be passed,
    /// this object can be set to null.
    /// </param>
    /// <returns>true if this command can be executed; otherwise, false.</returns>
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute();
    }
    /// <summary>
    /// Executes the RelayCommand on the current command target.
    /// </summary>
    /// <param name="parameter">
    /// Data used by the command. If the command does not require data to be passed,
    /// this object can be set to null.
    /// </param>
    /// 
    **private string btntag;
    public string BtnTag
    {
        get { return btntag; }
        set { btntag= value; }
    }**


    public void Execute(object parameter)
    {
         **btntag= parameter as string;**
        _execute();
    }
    /// <summary>
    /// Method used to raise the CanExecuteChanged event
    /// to indicate that the return value of the CanExecute
    /// method has changed.
    /// </summary>
    public void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
}

最后,您可以在命令中获取它:

private RelayCommand btnclickcommmand;

    public RelayCommand MyBtnClickCommand
    {
        get { return btnclickcommmand; }
        set { btnclickcommmand = value; }
    }

 btnclickcommmand = new RelayCommand(() =>
        {
            //TODO            
            mytag = btnclickcommmand.BtnTag;
            System.Diagnostics.Debug.WriteLine(mytag+"Button create the flyout");
        });