Может быть, этот подход поможет вам: он использует DataBinding (что должно быть в WPF) и дает вам возможность изменить ширину в codebehind.
XAML
<ScrollViewer>
<ScrollViewer.Resources>
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="Width" Value="{Binding MyWidth,Mode=OneWay}" />
</Style>
</ScrollViewer.Resources>
</ScrollViewer>
CodeBehind
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double myWidth;
public double MyWidth
{
get { return myWidth; }
set
{
if (value != this.myWidth)
{
myWidth = value;
NotifyPropertyChanged("MyWidth");
}
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
//set the width here in code behind
this.MyWidth = 200;
}
protected virtual void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Не забудьте реализовать INotifyPropertyChanged
- интерфейс