我怎麼設置的DataContext到,如果我想從一個類綁定屬性:綁定使用的DataContext
<Rectangle DataContext="Scaling" Height="{Binding VerticalSliderHeight}" Width="{Binding VerticalSliderHeight}">
<Rectangle.Fill>
<ImageBrush ImageSource="/Assets/Images/fader.png"/>
</Rectangle.Fill>
</Rectangle>
類:建議
public static class Scaling
{
//Just For Slider Resource
public static float VerticalSliderHeight { get; set; }
public static float VerticalSliderWidth { get; set; }
//
}
答案是WPF
更多信息在xaml端:
<Page.Resources>
<Style TargetType="Slider">
<Setter Property="Background" Value="{ThemeResource SystemControlForegroundBaseMediumLowBrush}" />
<Setter Property="BorderThickness" Value="{ThemeResource SliderBorderThemeThickness}" />
<Setter Property="Foreground" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="ManipulationMode" Value="None" />
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Slider">
<Grid Margin="{TemplateBinding Padding}">
<Grid.Resources>
<Style TargetType="Thumb" x:Key="SliderThumbStyleVertical">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="{ThemeResource SystemControlForegroundAccentBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Rectangle Height="60" Width="30">
<Rectangle.Fill>
<ImageBrush ImageSource="/Assets/Images/fader.png" Stretch="Uniform"/>
</Rectangle.Fill>
</Rectangle>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Thumb" x:Key="SliderThumbStyleHorizontal">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="{ThemeResource SystemControlForegroundAccentBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Rectangle x:Name="HorizontalThumbRect" Height="30" Width="60">
<Rectangle.Fill>
<ImageBrush ImageSource="/Assets/Images/fader2.png" Stretch="Uniform"/>
</Rectangle.Fill>
</Rectangle>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentPresenter x:Name="HeaderContentPresenter"
x:DeferLoadStrategy="Lazy"
Visibility="Collapsed"
Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}"
Margin="{ThemeResource SliderHeaderThemeMargin}"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
FontWeight="{ThemeResource SliderHeaderThemeFontWeight}"
TextWrapping="Wrap" />
<Grid x:Name="SliderContainer" Background="Transparent" Grid.Row="1" Control.IsTemplateFocusTarget="True">
<Grid x:Name="HorizontalTemplate" MinHeight="44">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0" />
<RowDefinition Height="Auto" />
<RowDefinition Height="0" />
</Grid.RowDefinitions>
<Rectangle x:Name="HorizontalTrackRect"
Fill="DimGray"
Height="{ThemeResource SliderTrackThemeHeight}"
Grid.Row="1"
Grid.ColumnSpan="3" />
<Rectangle x:Name="HorizontalDecreaseRect" Fill="DimGray" Grid.Row="1" />
<Thumb x:Name="HorizontalThumb"
Background="{ThemeResource SystemControlForegroundAccentBrush}"
Style="{StaticResource SliderThumbStyleHorizontal}"
DataContext="{TemplateBinding Value}"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="1"
AutomationProperties.AccessibilityView="Raw" />
</Grid>
<Grid x:Name="VerticalTemplate" MinWidth="44" Visibility="Collapsed">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="0" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="VerticalTrackRect"
Fill="DimGray"
Width="{ThemeResource SliderTrackThemeHeight}"
Grid.Column="1"
Grid.RowSpan="3" />
<Rectangle x:Name="VerticalDecreaseRect"
Fill="DimGray"
Grid.Column="1"
Grid.Row="2" />
<Thumb x:Name="VerticalThumb"
Background="{ThemeResource SystemControlForegroundAccentBrush}"
Style="{StaticResource SliderThumbStyleVertical}"
DataContext="{TemplateBinding Value}"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3"
AutomationProperties.AccessibilityView="Raw"/>
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
因此,似乎綁定在頁面資源內遇到了麻煩。有任何想法嗎?
這是uwp不是WPF – Brizzler
你想設置* DataContext *爲靜態類或任何類?它必須在XAML中或可以在代碼中?一般來說,你可以通過代碼來完成它 - 直接設置,通過綁定或通過資源來完成。 – Romasz
有點脫離主題,但爲什麼你使用舊的綁定?它們被x:Bind所取代。這是編譯的綁定。他們更快,消耗更少的內存,https://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension –