将RichTextBox绑定到C#中的Slider控件

时间:2021-06-07 16:03:34

I have the following XAML code that I want to perform in xaml.cs.

我有以下XAML代码,我想在xaml.cs.中执行。

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

Basically it binds the slider to the richtextbox and performs zooming.

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

The following is what i have attempted:

以下是我的尝试:

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

3 个解决方案

#1


3  

The following code behind is equivalent to the Xaml

后面的代码相当于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;

#2


1  

you did set the transform but not the binding - it will be fixed. You need to use something like

你确实设置了转换而不是绑定 - 它将被修复。你需要使用类似的东西

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

to really to the same

要真的到底一样

#3


1  

Not sure if you're asking how to perform the binding in code, or how to set the ScaleX and ScaleY properties in the code (e.g., without binding). If this is the case, here's how you'd do it:

不确定您是否询问如何在代码中执行绑定,或者如何在代码中设置ScaleX和ScaleY属性(例如,不绑定)。如果是这种情况,请按以下步骤操作:

First, give your ScaleTransform a name, e.g. "myScaleTransform":

首先,为您的ScaleTransform命名,例如“myScaleTransform”:

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

Then, add an event handler for the ValueChanged event of mySlider. In this handler, update the ScaleX and ScaleY properties of myScaleTransform:

然后,为mySlider的ValueChanged事件添加事件处理程序。在此处理程序中,更新myScaleTransform的ScaleX和ScaleY属性:

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

Hope this helps.

希望这可以帮助。

#1


3  

The following code behind is equivalent to the Xaml

后面的代码相当于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;

#2


1  

you did set the transform but not the binding - it will be fixed. You need to use something like

你确实设置了转换而不是绑定 - 它将被修复。你需要使用类似的东西

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

to really to the same

要真的到底一样

#3


1  

Not sure if you're asking how to perform the binding in code, or how to set the ScaleX and ScaleY properties in the code (e.g., without binding). If this is the case, here's how you'd do it:

不确定您是否询问如何在代码中执行绑定,或者如何在代码中设置ScaleX和ScaleY属性(例如,不绑定)。如果是这种情况,请按以下步骤操作:

First, give your ScaleTransform a name, e.g. "myScaleTransform":

首先,为您的ScaleTransform命名,例如“myScaleTransform”:

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

Then, add an event handler for the ValueChanged event of mySlider. In this handler, update the ScaleX and ScaleY properties of myScaleTransform:

然后,为mySlider的ValueChanged事件添加事件处理程序。在此处理程序中,更新myScaleTransform的ScaleX和ScaleY属性:

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

Hope this helps.

希望这可以帮助。