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

发布于 2024-11-30 13:47:38 字数 553 浏览 7 评论 0原文

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

<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;

I have the following XAML code that I want to perform in 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.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯了 2024-12-07 13:47:38

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

The following code behind is equivalent to the 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;
不乱于心 2024-12-07 13:47:38

您确实设置了变换,但没有设置绑定 - 它将被修复。
你需要使用类似的东西

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

来真正相同

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

与风相奔跑 2024-12-07 13:47:38

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

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

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

然后,为 ScaleTransform 添加一个事件处理程序a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.rangebase.valuechanged.aspx" rel="nofollow">mySliderValueChanged 事件。在此处理程序中,更新 myScaleTransformScaleXScaleY 属性:

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

希望这会有所帮助。

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:

First, give your ScaleTransform a name, e.g. "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:

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

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文