WPF Scrollviewer重置偏移量

时间:2016-01-18 12:48:52

标签: c# wpf dynamic scrollviewer

我在StackPanel内有一些编辑器,StackPanel包含一个ScrollViewer。滚动工作正常,height的{​​{1}}是根据窗口的高度动态设置的。 问题是,ScrollViewer如果点击ScrollViewer之一,或者用户选择其中一个VerticalOffset中的所有文字,RadioButtons会重置TextBoxes,因为没有包装和文本溢出TextBox的宽度。

StackPanel内的所有内容都是动态创建的。这是生成的XAML代码:

<ScrollViewer>
    <StackPanel Name="LeftEditorStack">
        <StackPanel Name="OuterPanelFirstname">
            <RadioButton Name="FirstnameEnabled"></RadioButton>
            <TextBox></TextBox>
        </StackPanel>
        <!--And many more editors to come-->
    </StackPanel>
</ScrollViewer>

这是生成的XAML代码的基本视图。这是三列之一,列看起来都是这样。

UI

如何防止此行为?

修改 以下是在for循环中生成控件的代码:

editorAttribute = (EditorAttribute)Attribute.GetCustomAttribute(properties[i], typeof(Weiss.WPF.Controls.Classes.EditorAttribute));
isEnabledAttribute = (IsEnabledAttribute)Attribute.GetCustomAttribute(properties[i], typeof(Weiss.WPF.Controls.Classes.IsEnabledAttribute));
displayAttribute = (DisplayAttribute)Attribute.GetCustomAttribute(properties[i], typeof(DisplayAttribute));

stackUId = Regex.Replace(Guid.NewGuid().ToString().Replace("-", String.Empty), @"[\d-]", String.Empty);

outerPanel = new StackPanel() { Orientation = Orientation.Vertical, DataContext = this, Name = String.Format("OuterPanel{0}{1}", displayAttribute.GroupName, stackUId) };

radioGroup = new RadioButton() { VerticalAlignment = VerticalAlignment.Center, GroupName = displayAttribute.GroupName, Name = String.Format("{0}Enabled{1}", displayAttribute.GroupName, stackUId), Content = displayAttribute.Name };
radioGroup.Checked += radioGroup_Checked;

editorBinding = new Binding();
editorBinding.Path = new PropertyPath(String.Format("DataContext.{0}", isEnabledAttribute.ElementName));

editorBinding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.FindAncestor, AncestorType = typeof(StackPanel), AncestorLevel = 2 };
radioGroup.SetBinding(RadioButton.IsCheckedProperty, editorBinding);

if (this.GetType().GetProperty(String.Format("{0}Enabled", displayAttribute.GroupName)) != null)
{
    radioGroup.IsChecked = this.GetType().GetProperty(isEnabledAttribute.ElementName).GetValue(this, null) is Boolean &&
                           (Boolean)this.GetType().GetProperty(isEnabledAttribute.ElementName).GetValue(this, null);
}

outerPanel.Children.Add(radioGroup);

editor = Activator.CreateInstance(editorAttribute.EditorType) as FrameworkElement;
editor.IsEnabled = false;
editorBinding = new Binding();
editorBinding.Path = new PropertyPath(String.Format("DataContext.{0}", properties[i].Name));
editorBinding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.FindAncestor, AncestorType = typeof(StackPanel), AncestorLevel = 2 };

editor.SetBinding(editorAttribute.BindingProperty, editorBinding);
//outerPanel is the LeftEditorStack StackPanel from above
outerPanel.Children.Add(editor);

编辑2:

经过进一步调查后,我注意到一些奇怪的事为了测试我的UI我使用这个类

public class Person {

    public String Firstname { get; set; }

    public String Lastname { get; set; }

    public DateTime Birthday { get; set; }

    public String Street { get; set; }

    public Int32 ZipCode { get; set; }

    public String Location { get; set; }

}

所有这些属性都绑定到文本框。如果我点击RadioButtons属性的String,则ScrollViewer会重置偏移量。对于其他属性,偏移保持原样。

0 个答案:

没有答案