我需要一个Viewport3D用于进行几何计算的唯一目的Petzold.Media3D.ViewportInfo.我宁愿不把它放在一个Window或以其他方式呈现它.
我试图通过Viewport3D使用以下C#方法实例化并设置一些属性来实现此目的:
private Viewport3D CreateViewport(MainSettings settings)
{
var cameraPosition = new Point3D(0, 0, settings.CameraHeight);
var cameraLookDirection = new Vector3D(0, 0, -1);
var cameraUpDirection = new Vector3D(0, 1, 0);
var camera = new PerspectiveCamera
{
Position = cameraPosition,
LookDirection = cameraLookDirection,
UpDirection = cameraUpDirection
};
var viewport = new Viewport3D
{
Camera = camera,
Width = settings.ViewportWidth,
Height = settings.ViewportHeight
};
return viewport;
}
Run Code Online (Sandbox Code Playgroud)
稍后,我正在尝试使用此视口使用此方法将鼠标位置转换为3D位置:
public Point3D? Point2dToPoint3d(Point point)
{
var range = …Run Code Online (Sandbox Code Playgroud) 我有一个Canvas高度为100和宽度为1920 的用户控件.
在加载控件时,我转到外部源,下载文本文件并将TextBlocks 添加到Canvas.然后我想创建一个选框滚动效果应该可以正常工作,除非我添加TextBlocks 后Canvas,我需要得到它们的宽度用于计算目的但ActualWidth属性始终为零.
这是一些代码:
private readonly LinkedList<TextBlock> textBlocks = new LinkedList<TextBlock>();
public LocalNewsControl()
{
Loaded += LocalNewsControlLoaded;
}
private void LocalNewsControlLoaded(object sender, RoutedEventArgs e)
{
LoadDataContext();
}
private void LoadDataContext()
{
DataContext = new NewsItemsViewModel((exception) => LoadNewsItems());
}
private void LoadNewsItems()
{
var viewModel = (NewsItemsViewModel)DataContext;
NewsCanvas.Children.Clear();
textBlocks.Clear();
foreach (var newsViewModel in viewModel.NewsItems)
{
var tb = new TextBlock
{
Text = newsViewModel.Headline,
FontSize = 28,
FontWeight = …Run Code Online (Sandbox Code Playgroud) 我无法获得ActualWidth任何非零值的画布。我简化了真实场景,并在 VS 中使用了一个新的 WPF 应用程序来获取非零值(和理解)。不幸的是,我只得到零。我觉得我缺少一些基本的 WPF 理解:我对 WPF 不太熟悉。
我复制了MDSN 演示,稍微修改了一下,现在我有了
public partial class MainWindow : Window {
private Canvas myCanvas;
public MainWindow()
{
InitializeComponent();
CreateAndShowMainWindow();
}
private void CreateAndShowMainWindow()
{
// Create a canvas sized to fill the window
myCanvas = new Canvas();
myCanvas.Background = Brushes.LightSteelBlue;
// Add a "Hello World!" text element to the Canvas
TextBlock txt1 = new TextBlock();
txt1.FontSize = 14;
txt1.Text = "Hello World!";
Canvas.SetTop(txt1, 100);
Canvas.SetLeft(txt1, 10);
myCanvas.Children.Add(txt1);
// Add …Run Code Online (Sandbox Code Playgroud) 在WPF中,我想设置一个控件宽度,比如它的父控件ActualWidth属性的97%.我怎样才能做到这一点?
如何将ActualWidth用户控件的某个组件的属性公开给用户?
我已经找到了很多关于如何通过创建新的依赖项属性和绑定来公开普通属性的示例,但是没有关于如何公开只读属性的示例ActualWidth.
将其粘贴到苹果酒中.
<Grid x:Name="Grid">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition"/>
</Grid.ColumnDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding ActualWidth, ElementName=Grid}"/>
<TextBlock Text="{Binding ActualWidth, ElementName=ColumnDefinition}"/>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
现在,运行它.第二个TextBlock显示0,但不应该,对吧?解决方法?
为什么我不能将其ActualWidth称为值,我可以在代码中使用它.
XAML:
<StackPanel>
<Button x:Name="cmdVibrate" HorizontalAlignment="Center">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualWidth}" By="200" AutoReverse="True" Duration="0:0:1" Storyboard.TargetProperty="Width"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
Mouse over me to shake
</Button>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud) 我有Canvas一个TextBlock像这样:
<Canvas x:Name="ContentPanel" Grid.Row="1" DoubleTapped="ContentPanel_DoubleTapped">
<TextBlock x:Name="WordBlock" FontSize="226.667" FontFamily="Segoe UI Semilight" TextAlignment="Center"
RenderTransformOrigin="0.5, 0.5">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="translate"/>
</TextBlock.RenderTransform>
</TextBlock>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
我的应用程序是这样的,当用户导航到此页面时,TextBlock将以中心为中心Canvas,如果TextBlock宽度大于画布的宽度,则会出现选框动画:
private void SetAnimation()
{
Canvas.SetLeft(WordBlock, (ContentPanel.ActualWidth - WordBlock.ActualWidth) / 2);
Canvas.SetTop(WordBlock, (ContentPanel.ActualHeight - WordBlock.ActualHeight) / 2);
if (WordBlock.ActualWidth > ContentPanel.ActualWidth)
{
MarqueeAnimation.From = WordBlock.ActualWidth;
MarqueeAnimation.To = -WordBlock.ActualWidth;
MarqueeAnimation.Duration = new Duration(new TimeSpan(0, 0, 10));
MarqueeBoard.Begin();
}
}
Run Code Online (Sandbox Code Playgroud)
此方法称为OnNavigatedTo.我无法弄清楚为什么TextBlock不会居中,因为ActualHeight和ActualWidth属性总是以0.0的形式返回.我不想放置固定大小,因为这是一个Windows应用商店应用程序,并希望它可以在不同的屏幕尺寸上进行扩展.
有任何想法吗?我被卡住了.
我试图创建一个自定义控件,具有半透明的圆形背景:
<Canvas>
<TextBlock x:Name="StopText" Text="Some test text"/>
<Rectangle Fill="SkyBlue"
Width="{Binding Source=StopText, Path=ActualHeight}"
Height="20"
RadiusX="5" RadiusY="5" Opacity="0.2"/>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
问题是我不能绑定到ActualHeight/ ActualWidthproperties,因为它们不是依赖项.
如何处理相同大小的矩形和文本框?
我对如何计算ActualWidth或ActualHeight工作或如何计算感到困惑.
<Ellipse Height="30" Width="30" Name="rightHand" Visibility="Collapsed">
<Ellipse.Fill>
<ImageBrush ImageSource="Images/Hand.png" />
</Ellipse.Fill>
</Ellipse>
Run Code Online (Sandbox Code Playgroud)
当我使用上面的代码时,我得到30 ActualWidth和ActualHeight.但是当我以编程方式定义一个椭圆时,即使我定义了(最大)高度和(最大)宽度属性,ActualWidth并且ActualHeight为0,我不明白它是如何为0的?
actualwidth ×10
wpf ×9
actualheight ×4
c# ×4
binding ×3
wpf-controls ×2
xaml ×2
.net ×1
3d ×1
controls ×1
grid ×1
layout ×1
readonly ×1
sizing ×1
width ×1