在c#中将RichTextBox绑定到滑块控件



我有下面的XAML代码,我想在XAML .cs中执行。

<RichTextBox.LayoutTransform>
    <ScaleTransform ScaleX="{Binding ElementName=mySlider, Path=Value}"
                    ScaleY="{Binding ElementName=mySlider, Path=Value}"/>
</RichTextBox.LayoutTransform>

基本上它将滑动条绑定到richtextbox并执行缩放。

下面是我尝试过的:

RichTextBox newtext = new RichTextBox();
ScaleTransform mytran = new ScaleTransform();
mytran.ScaleX = mySlider.Value;
mytran.ScaleY = mySlider.Value;
newtext.LayoutTransform = mytran;

后面的代码相当于Xaml

//<RichTextBox.LayoutTransform>
//    <ScaleTransform ScaleX="{Binding ElementName=mySlider, Path=Value}"
//                    ScaleY="{Binding ElementName=mySlider, Path=Value}"/>
//</RichTextBox.LayoutTransform>
ScaleTransform scaleTransform = new ScaleTransform();
Binding scaleXBinding = new Binding("Value");
scaleXBinding.Source = mySlider;
Binding scaleYBinding = new Binding("Value");
scaleYBinding.Source = mySlider;
BindingOperations.SetBinding(scaleTransform,
                             ScaleTransform.ScaleXProperty,
                             scaleXBinding);
BindingOperations.SetBinding(scaleTransform,
                             ScaleTransform.ScaleYProperty,
                             scaleYBinding);
RichTextBox newText = new RichTextBox();
newText.LayoutTransform = scaleTransform;

您确实设置了转换,但没有设置绑定-它将被修复。你需要使用像

这样的东西
Binding scaleBinding = new Binding("Value"){ElementName="mySlider"};
BindingOperations.SetBinding(mytran, ScaleTransform.ScaleXProperty, scaleBinding);
BindingOperations.SetBinding(mytran, ScaleTransform.ScaleYProperty, scaleBinding);

不确定您是否在询问如何在代码中执行绑定,或者如何在代码中设置ScaleXScaleY属性(例如,不绑定)。如果是这种情况,可以这样做:

首先,给你的ScaleTransform一个名字,例如:myScaleTransform":

<RichTextBox.LayoutTransform>
   <ScaleTransform x:Name="myScaleTransform" ScaleX="1" ScaleY="1" />
</RichTextBox.LayoutTransform>

然后,为mySliderValueChanged事件添加事件处理程序。在这个处理程序中,更新myScaleTransformScaleXScaleY属性:

public void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    myScaleTransform.ScaleX = mySlider.Value;
    myScaleTransform.ScaleY = mySlider.Value;
}

相关内容

  • 没有找到相关文章

最新更新