WPF 形状矩形绑定
我正在尝试在 wpf 中制作某种形状,它会根据内容(应该是文本)调整自身大小。 不幸的是,拉伸属性不是正确的,因为我只想调整形状的宽度,并且不拉伸该形状的边框(请复制 xamlpad 中的底部示例以供自己查看)。 边框应保持原样,或者至少按统一比例缩放。 我尝试过很多想法。 在网格、堆栈面板或剪切面板等中使用不同的形状切片。 我的下一个方法将是以下方法:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Page.Resources>
<LinearGradientBrush StartPoint="0.0,1" EndPoint="0.0,0" x:Key="brushYellow">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.000000" Color="#fffef4a6"/>
<GradientStop Offset="0.175824" Color="#fffef9d6"/>
<GradientStop Offset="0.800000" Color="#fffef9d6"/>
<GradientStop Offset="1.000000" Color="#fffef4a6"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush></Page.Resources><Grid>
<Path Stroke="#fffce90d" StrokeThickness="1" Fill="{StaticResource brushYellow}">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Exclude">
<CombinedGeometry.Geometry1>
<RectangleGeometry RadiusX="15" RadiusY="15">
<!--RectangleGeometry.Rect>
<Binding StringFormat="{}{0 0 {0} 82}" ElementName="Text" Path="Width"/>
</RectangleGeometry.Rect-->
<RectangleGeometry.Rect>
<Rect Width="150" Height="82"/>
</RectangleGeometry.Rect>
</RectangleGeometry>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure IsClosed="True" StartPoint="0,15">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="17,41" />
<LineSegment Point="0,67" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
<TextBox Name="Text" Background="Transparent" BorderThickness="0" MinWidth="150" Margin="0"/>
</Grid></Page>
这将在 xamlpad 中开箱即用。 第19行未注释的部分是我真正想要实现的:将矩形的矩形绑定到其他东西。 不幸的是,矩形的宽度不是 dp,这就是为什么我直接使用字符串格式的绑定到矩形本身。
正如生活所预期的那样,这是行不通的(什么都看不见):D 我在这里做错了什么?
I'm trying to make some kind of shape in wpf, which does resize itself to the content (which should be text). Unfortunately, the stretch property isn't the right thing, since I want only the width of the Shape resized and without the borders (pls copy bottom example in xamlpad to see for yourself) of this shape stretched. The borders should stay the way they are, or at least scale in Uniform.
I've tried many ideas. With different slices of the shape in a grid, stackpanel, or with a clipped panel and etc.
My next approach would be the following one:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Page.Resources>
<LinearGradientBrush StartPoint="0.0,1" EndPoint="0.0,0" x:Key="brushYellow">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.000000" Color="#fffef4a6"/>
<GradientStop Offset="0.175824" Color="#fffef9d6"/>
<GradientStop Offset="0.800000" Color="#fffef9d6"/>
<GradientStop Offset="1.000000" Color="#fffef4a6"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush></Page.Resources><Grid>
<Path Stroke="#fffce90d" StrokeThickness="1" Fill="{StaticResource brushYellow}">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Exclude">
<CombinedGeometry.Geometry1>
<RectangleGeometry RadiusX="15" RadiusY="15">
<!--RectangleGeometry.Rect>
<Binding StringFormat="{}{0 0 {0} 82}" ElementName="Text" Path="Width"/>
</RectangleGeometry.Rect-->
<RectangleGeometry.Rect>
<Rect Width="150" Height="82"/>
</RectangleGeometry.Rect>
</RectangleGeometry>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure IsClosed="True" StartPoint="0,15">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="17,41" />
<LineSegment Point="0,67" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
<TextBox Name="Text" Background="Transparent" BorderThickness="0" MinWidth="150" Margin="0"/>
</Grid></Page>
This will work right out of the box in xamlpad. The uncommented part at line 19 is what I really want to achieve: Binding the Rect of the Rectangle to something else. Unfortunately, the Width of Rect isn't dp, that's why I'm using directly a stringformatted Binding to Rect itself.
As expected with life, that doesn't work (nothing is visible) :D
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用一组名为 ViewboxPath、ViewboxLine、ViewboxPolyline 等的类,这些类将 Shape 的拉伸语义更改为更易于处理。 我不确定我是否理解你的问题,所以我不知道我的技术是否能解决你的问题。
当我读到它时,要么您想要控制拉伸(该解决方案将提供),要么您希望笔划与图像一起拉伸(山姆的答案将提供)。
无论如何,下面是这些类的代码,这就是您使用它们的方式:
我的Viewbox形状类就像普通形状一样使用(
Polyline
,Polygon、
Path
和Line
),除了额外的Viewbox
参数,以及它们默认为Stretch="Fill" 的事实
。 Viewbox 参数指定在用于指定形状的坐标系中,应使用Fill
、Uniform
或UniformToFill
拉伸的几何图形区域> 设置,而不是使用Geometry.GetBounds
。这可以非常精确地控制拉伸,并且可以轻松地将单独的形状彼此精确对齐。
以下是我的 Viewbox 形状类 的实际代码,包括包含常见功能的抽象基类
ViewboxShape
:享受吧!
I use a set of classes named ViewboxPath, ViewboxLine, ViewboxPolyline, etc that change the stretch semantics of Shape to be a little more tractable. I'm not sure I understood your question, so I don't know if my technique will solve your problem or not.
As I read it, either you want control over the stretching, which this solution will provide, or you want to have the strokes stretch along with the image, which Sam's answer will provide.
Anyhow, below is the code for these classes and this is how you use them:
My Viewbox shape classes are used just like normal shapes (
Polyline
,Polygon
,Path
andLine
) except for the extraViewbox
parameter, and the fact that they default toStretch="Fill"
. The Viewbox parameter specifies, in the coordinate system used to specify the shape, the area of the geometry that should be stretched usingFill
,Uniform
orUniformToFill
settings, instead of usingGeometry.GetBounds
.This gives very precise control over the stretching and makes it easy to have separate shapes align accurately with one another.
Here is the actual code for my Viewbox shape classes, including the abstract base class
ViewboxShape
that contains common functionality:Enjoy!
您可以尝试使用变换来更改矩形的大小,而不是直接绑定矩形的宽度。 我认为这应该有效。
例如,在您的 RectangleGeometry 标记中放入类似的内容:
其中 textBoxName 是文本框的名称。 无法让自己称其为“文本”——太混乱了。
您将需要提供一个转换器以确保缩放正确 - 例如,根据示例代码,您可能希望返回类似 Width / 150 的内容。
当在 Visual Studio 设计器中将矩形的宽度设置为“自动”时,我看到稍微奇怪的行为 - 我认为这可能是设计器的怪癖。 一旦转换器在运行时连接起来就应该可以工作。
You could try using a transform to change the size of the rectangle rather than binding the rectangle's width directly. I think this should work.
E.g. put something like this in your RectangleGeometry tag:
Where textBoxName is the name of your textbox. Couldn't bring myself to call it Text - too confusing.
You will need to supply a converter to ensure the scaling is correct - e.g. You'll probably want to return something like Width / 150 given your sample code.
I am seeing slightly odd behaviour when the rectangle's width is set to Auto in the Visual Studio designer - I think this is probably a designer quirk. Should work once the converter is hooked up at run time.
使用你的路径作为画笔怎么样? 在下面的代码中,我使用 DrawingBrush 作为 TextBox 本身的背景或作为封闭边框的背景。 只是一个提示...
希望这可以帮助。
What about using your path as Brush? In the following code I use a DrawingBrush as Background for the TextBox itself or as Background for an enclosing Border. Just an hint...
Hope this helps.
我正在做这样的事情。 我想要自定义形状,当我调整窗口大小时自动调整大小。 对于我的解决方案,我从形状派生并覆盖定义几何属性。
对于形状的尺寸,我使用 ActualWidth 和 ActualHeight 属性,因为它们反映了真实的宽度和高度。 我还像这样覆盖了measureOverride()方法,
我使用(就像我之前说的)actualWidth和actualHeight作为最大值在代码中生成形状。
希望这可以帮助
i'm doing something like this. i want custom shapes that automatically re size when i resize the window. for my solution i derived from shape and overridden the definingGeometry property.
For the dimensions of my shape i use the ActualWidth and ActualHeight properties as these reflect the true width and height. i'm also overiding the measureOverride() method like this
I'm generating the shape in code using (like i said before) actualWidth and actualHeight as maximum values.
hope this helps