我的窗口代码后面定义了一个依赖属性“Active”...
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }
public bool Active
{
get { return (bool) GetValue(ActiveProperty); }
set { SetValue(ActiveProperty, value); }
}
public static readonly DependencyProperty ActiveProperty =
DependencyProperty.Register("Active", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));
}
Run Code Online (Sandbox Code Playgroud)
然后我使用 xaml 中的两个复选框绑定到该属性。我还想根据该属性更改矩形的填充。我怎样才能做到这一点?
<Window x:Class="WpfTest.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"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<CheckBox IsChecked="{Binding Active}" />
<CheckBox IsChecked="{Binding Active}" />
<Rectangle Fill="Gray"
Width="50"
Height="50">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Active}"
Value="True">
<Setter Property="Fill"
Value="Green" />
</DataTrigger> …Run Code Online (Sandbox Code Playgroud) 我有一个带有Button和ListBox的窗口.
<Window x:Class="ListBoxFail.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Grid>
<Popup Name="Popup" StaysOpen="False">
<Rectangle Fill="Blue" Width="100" Height="100"/>
</Popup>
<Button Content="ClickMe" Click="OpenPopup" VerticalAlignment="Top"/>
<ListBox SelectionChanged="OpenPopup" VerticalAlignment="Bottom">
<ListBoxItem Content="One"/>
<ListBoxItem Content="Two"/>
</ListBox>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
Button和ListBox都会触发Popup打开:
namespace ListBoxFail {
partial class MainWindow {
public MainWindow() { InitializeComponent(); }
private void OpenPopup(object a, object b) { Popup.IsOpen = true; }
}
}
Run Code Online (Sandbox Code Playgroud)
单击该按钮时,将打开弹出窗口.单击其他任何位置然后按预期关闭弹出窗口.
单击ListBox时,将打开弹出窗口.但是,此时关闭弹出窗口的唯一方法是让主窗口失去焦点.单击窗口或单击按钮不会关闭弹出窗口.
是什么赋予了?为什么ListBox忽略了StaysOpen = false指令?我究竟做错了什么?
在我的应用程序中,我有一个颜色资源.我有一个元素使用该颜色作为xaml中的动态资源.
<Window x:Class="ResourcePlay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="425">
<Window.Resources>
<Color x:Key="MyColor">Red</Color>
</Window.Resources>
<Grid>
<Rectangle VerticalAlignment="Top" Width="80" Height="80" Margin="10">
<Rectangle.Fill>
<SolidColorBrush x:Name="TopBrush" Color="{DynamicResource MyColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle VerticalAlignment="Bottom" Width="80" Height="80" Margin="10">
<Rectangle.Fill>
<SolidColorBrush x:Name="BottomBrush"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
在代码中,我想复制此资源引用.
using System.Windows;
using System.Windows.Media;
namespace ResourcePlay {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
// I want to copy the resource reference, not the color.
BottomBrush.Color = TopBrush.Color;
// I'd really rather do something like this.
var reference …Run Code Online (Sandbox Code Playgroud)