我有这个停靠面板,我正在尝试为其设置绑定。
问题是我的 XAML 的 DataContext 设置为 MainWindow 类,但我要绑定到的类(autoCam)与该类分开。我在 MainWindow 类中使用了一个 autoCam 实例,称为“myCam”,但我对如何使这些绑定起作用感到困惑。
这是我在停靠面板中使用的代码:
<DockPanel x:Name="BottomDock" Visibility="Collapsed" Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="2" DockPanel.Dock="Bottom" Margin="0">
<Button x:Name="Rewind" Click="Rewind_Click" HorizontalAlignment="Left" Width="23" RenderTransformOrigin="0.5,0.5" Grid.Row="1"/>
<Button x:Name="Play" Content=">" Click="Play_Click" HorizontalAlignment="Left" Width="17" RenderTransformOrigin="0.5,0.5" Grid.Row="1"/>
<Button x:Name="Pause" Content="||" Click ="Pause_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Width="16" Height="22" RenderTransformOrigin="0.5,0.5" Grid.Row="1"/>
<Button x:Name="FastForward" Content=">>" Click="FastForward_Click" HorizontalAlignment="Left" Width="23" RenderTransformOrigin="0.5,0.5" Grid.Row="1"/>
<TextBlock Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="95" Text="Camera Position"/>
<TextBox Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="40" Name="cpx" Text="{Binding Path=myCam.cameraPositionX, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="40" Name="cpy" Text="{Binding Path=myCam.cameraPositionY, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="40" Name="cpz" Text="{Binding Path=myCam.cameraPositionZ, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="95" Text="Look Direction"/>
<TextBox Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="40" Name="ldx" Text="{Binding Path=myCam.lookDirectionX, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="40" Name="ldy" Text="{Binding Path=myCam.lookDirectionY, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Background="Black" TextAlignment="Center" Foreground="AliceBlue" HorizontalAlignment="Left" Width="40" Name="ldz" Text="{Binding Path=myCam.lookDirectionZ, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
起初我以为我可以将 DataContext=autoCam 作为停靠面板本身的属性,或者为每个单独的文本框编写,然后只说 {binding cameraPositionX} 或其他什么,但这没有做任何事情。
然后我想我可以说 {Binding myCam.cameraPositionX} 因为 myCam 是 MainWindow 类的成员。但这也不起作用。
在一次徒劳的尝试中,我试图将我的猜测结合起来,并将 DataContext=autoCam 添加到所有文本框的属性中,但我从未想过这会真正起作用。
我也尝试在 datacontext=this; 之后写以下内容;在主窗口构造函数中的行。我尝试了这个,以防问题是 xaml 会按照我的需要正确设置数据上下文,但是当构造函数运行时,整个 ui 的数据上下文将重新应用于主窗口。
public MainWindow()
{
InitializeComponent();
Interval = new TimeSpan(0, 0, 0, 0, 10);
Rewind.Content = "<<";
runType = 2;
timerRate(runType);
Loaded += new RoutedEventHandler(MainWindow_Loaded);
DataContext = this;
cpx.DataContext = myCam;
cpy.DataContext = myCam;
cpx.DataContext = myCam;
ldx.DataContext = myCam;
ldy.DataContext = myCam;
ldz.DataContext = myCam;
//view1.IsHeadLightEnabled = true;
}
我想也许我可以把它放在 Window.Resources XAML 中:
<Window.Resources>
<ObjectDataProvider x:Key="Camera" ObjectType="{x:Type local:autoCam}" MethodName="getCamDetails"/>
</Window.Resources>
但是我在我的 autoCam 类中编写了我的方法 getCamDetails 如下,每次我尝试将 CamDetails[0] 设置为 cameraPositionX 时,它都会给我一个空引用异常。
public class autoCam : INotifyPropertyChanged
{
MainWindow mW;
public static double[] CamDetails;
public static double[] getCamDetails()
{
return CamDetails;
}
public Point3D cameraPosition
{
get { return mC.Position; }
set
{
mC.Position = value;
cameraPositionX = Convert.ToDouble(cameraPosition.X);
cameraPositionY = Convert.ToDouble(cameraPosition.Y);
cameraPositionZ = Convert.ToDouble(cameraPosition.Z);
lookDirection = mC.LookDirection;
lookDirectionX = Convert.ToDouble(lookDirection.X);
lookDirectionY = Convert.ToDouble(lookDirection.Y);
lookDirectionZ = Convert.ToDouble(lookDirection.Z);
onPropertyChanged("cameraPosition");
}
}
public double cameraPositionX
{
get { return Convert.ToDouble(cameraPosition.X); }
set { onPropertyChanged("cameraPositionX"); CamDetails[0] = cameraPositionX; }
}
public double cameraPositionY
{
get { return Convert.ToDouble(cameraPosition.Y); }
set { onPropertyChanged("cameraPositionY"); CamDetails[1] = cameraPositionY; }
}
public double cameraPositionZ
{
get { return Convert.ToDouble(cameraPosition.Z); }
set { onPropertyChanged("cameraPositionZ"); CamDetails[2] = cameraPositionZ; }
}
public Vector3D lookDirection
{
get { return mC.LookDirection; }
set {
mC.LookDirection = value;
lookDirectionX = Convert.ToDouble(lookDirection.X);
lookDirectionY = Convert.ToDouble(lookDirection.Y);
lookDirectionZ = Convert.ToDouble(lookDirection.Z);
onPropertyChanged("lookDirection");
}
}
public double lookDirectionX
{
get { return Convert.ToDouble(lookDirection.X);}
set { onPropertyChanged("lookDirectionX"); CamDetails[3] = lookDirectionX; }
}
public double lookDirectionY
{
get { return Convert.ToDouble(lookDirection.Y); }
set { onPropertyChanged("lookDirectionY"); CamDetails[4] = lookDirectionY; }
}
public double lookDirectionZ
{
get { return Convert.ToDouble(lookDirection.Z); }
set { onPropertyChanged("lookDirectionZ"); CamDetails[5] = lookDirectionZ; }
我真的很感激一个解决方案,但是从长远来看,分解为什么这些对我不起作用从本质上来说会对我有更多帮助。
感谢您的时间并帮助大家。
最好也共享 MainWindow 类,但听起来像在 MainWindow 类中放置一个属性来公开 myCam 实例并直接绑定到该属性应该可以工作。
public partial class MainWindow : Window
{
private AutoCam autoCam;
public AutoCam AutoCam
{
get { return autoCam; }
set { autoCam = value; }
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.autoCam = new AutoCam();
}
}
public class AutoCam
{
private string someText;
public string SomeText
{
get { return someText; }
set { someText = value; }
}
public AutoCam()
{
this.SomeText = "Some Text";
}
}
绑定将是这样的。
<Label Content="{Binding AutoCam.SomeText}">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句