簡體   English   中英

將不透明度值綁定到靜態屬性;

[英]Binding Opacity values to a static property;

我正在嘗試制作一個桌面疊加應用程序(想想雨表),並且由於背景可以更改,因此我希望能夠更改應用程序中文本的常規顏色和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

您可以使用x:Static綁定靜態屬性,但x:Static問題是它們不支持屬性更改機制,即,如果靜態屬性發生更改,則不會在UI上進行更新。

但是,在WPF 4.5中,您可以通過使用事件StaticPropertyChanged來支持該功能。 您只需要確保每當靜態屬性發生更改時,就引發此事件,以便更新UI。

同樣,綁定靜態屬性的語法在您的情況下有點不同:

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

樣本可以在這里找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM