【发布时间】:2012-04-02 00:00:52
【问题描述】:
我很难设置我认为应该很容易的绑定。非常感谢您的帮助。
我有一个名为 FormResource.xaml 的资源字典。在这本字典中包含我重新编写模板的 ScrollView 的样式。目的是我想要一个更宽的垂直滚动条。
<Style x:Key="LargeScrolling" TargetType="ScrollViewer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollViewer">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollContentPresenter x:Name="ScrollContentPresenter"
Margin="{TemplateBinding Padding}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>
<ScrollBar x:Name="PART_VerticalScrollBar"
Style="{StaticResource LargeVerticalScrollBar}"
Width="{Binding ElementName=MDTForm, Path=ScrollBarWidth}"
IsTabStop="False"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Grid.Column="1" Grid.Row="0" Orientation="Vertical"
ViewportSize="{TemplateBinding ViewportHeight}"
Maximum="{TemplateBinding ScrollableHeight}"
Minimum="0"
Value="{TemplateBinding VerticalOffset}"
Margin="0,-1,-1,-1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有一个名为 FormControl 的用户控件。
public class FormControl : UserControl
我曾经把它作为一个带有 XAML 组件的部分类,我试图在其中工作,但我不得不删除 XAML,因为我从另一个程序集中的这个类派生,而 WPF 不允许你从另一个程序集中的部分类派生。
在 FormControl 中,我定义了一个 ScrollBarWidth 属性。
public static readonly DependencyProperty ScrollBarWidthProperty = DependencyProperty.Register("ScrollBarWidth", typeof(double), typeof(FormControl));
public double ScrollBarWidth
{
get { return (double)base.GetValue(ScrollBarWidthProperty); }
set { base.SetValue(ScrollBarWidthProperty, value); }
}
当我在主声明中将此作为部分类时,我为 FormControl 类指定了 MDTForm 的名称,这就是我在绑定中用作 ElementName 的名称。我尝试在 FormClass.cs 中注册此名称,但无论我做什么,滚动条都不会获取属性值。
这里是我在 FormControl 类中创建 ScrollViewer 的地方。
_canvasScrollViewer = new ScrollViewer();
_canvasScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
_canvasScrollViewer.VerticalAlignment = VerticalAlignment.Top;
_canvasScrollViewer.MaxHeight = Constants.ScrollViewMaxHeight;
_canvasScrollViewer.Style = (Style)FindResource("LargeScrolling");
我让它工作的唯一方法是绑定到一个静态属性。我用这个来装订。
Width="{Binding Source={x:Static form:FormControl.ScrollBarWidthP}}"
然后这样定义属性。
public static double ScrollBarWidth { get; set; }
但是,我不希望这样,因为我可以同时加载多个 FormControl 对象,而且它们可能不都具有相同的滚动条宽度属性。
【问题讨论】:
标签: wpf xaml binding resources properties