【问题标题】:Binding Opacity values to a static property;将不透明度值绑定到静态属性;
【发布时间】:2014-08-18 13:21:21
【问题描述】:

我正在尝试制作一个桌面覆盖应用程序(想想雨量计),因为背景可以改变,我希望能够改变应用程序中文本的一般颜色和 alpha 值。

所以在设置菜单中,我想要一个 OnValueChanged 在静态类中设置属性的滑块,并且许多控件的不透明度绑定到该属性。为了使这更加复杂(也许?),应用程序同时打开了多个窗口。我没有绑定经验,无法使用。

到目前为止我的代码:

VisualSettings.cs

namespace ProjectSideBar
{
    public  class VisualSettings
    {
        public static double Opacity { get; set; }
    }
}

MainWindow.xaml

<Window x:Class="ProjectSideBar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:PSB="clr-namespace:ProjectSideBar"
        Title="MainWindow"  Height="1080" Width="300" ResizeMode="NoResize" ShowInTaskbar="False" WindowStyle="None" Closing="Window_Closing_1" Loaded="Window_Loaded" Background="Transparent"  >
    <Window.Resources>
        <PSB:VisualSettings x:Key="VisualSettings"/>
    </Window.Resources>

    <Grid>
        <TextBlock x:Name="ClockTB" HorizontalAlignment="Left" TextWrapping="Wrap" Text="22:22:22" VerticalAlignment="Top" Height="84" Width="300" Cursor="None" Foreground="White" FontSize="48" FontFamily="BatmanForeverAlternate" TextAlignment="Center" Opacity="{Binding Source={StaticResource VisualSettings} , Path=Opacity}" Margin="0,22,0,0" RenderTransformOrigin="0.5,0.5">
            <TextBlock.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="1.5"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBlock.RenderTransform>
        </TextBlock>


        <Slider x:Name="TestSlider" HorizontalAlignment="Left" Margin="10,1052,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.611" Width="172" Foreground="#FF122268" ValueChanged="TestSlider_ValueChanged" LargeChange="0.1" SmallChange="0.01" Maximum="1" Value="0.65"/>
    </Grid>
</Window>

MainWindow.xaml.cs

private void TestSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    VisualSettings.Opacity = TestSlider.Value;

}

谁能帮帮我?

亲切的问候,RoXaS

【问题讨论】:

  • 此链接here 将帮助您入门。
  • @RohitVats 谢谢,完美运行。如果您将此添加为答案,我会接受。

标签: c# wpf xaml binding


【解决方案1】:

您可以使用 x:Static 绑定静态属性,但 x:Static 的问题是它们不支持属性更改机制,即如果静态属性更改,则不会在 UI 上更新.

但是,在 WPF 4.5 中,您可以通过使用事件 StaticPropertyChanged 来支持这一点。您只需要确保每当静态属性发生更改时您都会引发此事件,以便 UI 得到更新。

在你的情况下,绑定静态属性的语法也有点不同:

"{Binding Path=(local:VisualSettings.Opacity), Mode=TwoWay}"

样品可以在here找到。

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 2010-10-30
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多