如何在 UWP 应用程序的调用弹出按钮中将按钮内容获取为 属性?
How to get Buttons content as property in a called flyout in an UWP app?
用例
我有一堆 (8) 个按钮可以通过设置样式 属性 打开天桥。
天桥包含一个 "Apply" 按钮。该按钮将触发一个命令,该命令在视图模型中执行与源按钮内容(1 到 8)相关的操作。
问题
如何获取触发 Flyover 到我的命令的按钮的内容值?
我认为 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"/>
但没有起到积极作用。
我不太确定我是否正确理解了您的要求。你真的是这个意思吗,你有 8 个按钮,每个按钮都可以升起弹出按钮。所以您想知道哪个按钮实际引发了弹出窗口?如果这是你想要的,也许你可以试试下面的代码:
首先,每个 FramewrokElement 都有一个 属性 调用标签。所以这是一个演示 XAML:
<Grid>
<Button Tag="button no.1" Command="{Binding MyBtnClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=Tag}" Content="set empty content"/>
</Grid>
那么如果你使用的是 RelayCommand,我们可以从这个 class:
中获取标签值
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");
});
用例
我有一堆 (8) 个按钮可以通过设置样式 属性 打开天桥。
天桥包含一个 "Apply" 按钮。该按钮将触发一个命令,该命令在视图模型中执行与源按钮内容(1 到 8)相关的操作。
问题
如何获取触发 Flyover 到我的命令的按钮的内容值?
我认为 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"/>
但没有起到积极作用。
我不太确定我是否正确理解了您的要求。你真的是这个意思吗,你有 8 个按钮,每个按钮都可以升起弹出按钮。所以您想知道哪个按钮实际引发了弹出窗口?如果这是你想要的,也许你可以试试下面的代码:
首先,每个 FramewrokElement 都有一个 属性 调用标签。所以这是一个演示 XAML:
<Grid>
<Button Tag="button no.1" Command="{Binding MyBtnClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=Tag}" Content="set empty content"/>
</Grid>
那么如果你使用的是 RelayCommand,我们可以从这个 class:
中获取标签值 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");
});