2

我有一个Viewport3D我想在两个照明条件之间交替。每个光照条件都将Model3DGroup包含一个或多个光照(DirectionalLightAmbientLight等)。

我打算使用DataTrigger,但我不知道应该使用哪个对象和哪些属性,因为Model3DGroup它的直接父级 ( ModelVisual3D) 没有Style属性。如果我StyleViewport3D自身中使用Setter.TargetName属性,则无法识别目标,如下面的代码所示:

<CheckBox x:Name="shadows" Content="Shadows" />
<Viewport3D>
    <Viewport3D.Resources>
        <Model3DCollection x:Key="shaded_light">
            <DirectionalLight Color="White" Direction="-1,-1, -10" />
        </Model3DCollection>
        <Model3DCollection x:Key="solid_light">
            <AmbientLight  Color="White" />
        </Model3DCollection>
    </Viewport3D.Resources>
    <Viewport3D.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=shadows, Path=IsChecked}" Value="True">
                    <!-- The setter below doesn't work: "The name 'lightsource' is not recognized" -->
                    <Setter TargetName="lightsource" Property="Content" Value="{StaticResource shaded_light}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Viewport3D.Style>
    <Viewport3D.Camera>
        <OrthographicCamera LookDirection="0,0,-1" Position="0,0,500" UpDirection="0,1,0" Width="400"/>
    </Viewport3D.Camera>
    <ModelVisual3D x:Name="lightsource">
        <ModelVisual3D.Content>
            <Model3DGroup Children="{StaticResource solid_light}" />
        </ModelVisual3D.Content>
    </ModelVisual3D>
    (...)
4

1 回答 1

0

由于ViewPort3D(有Style属性但没有属性Template)的一些限制,我不得不求助于代码隐藏以便在不同的光源之间切换。我将在下面发布相关部分,(这次使用葡萄牙语的变量名):

(注意:我仍然更喜欢使用绑定的仅 XAML 解决方案,但我现在无法弄清楚。如果有人发布一些仅 XAML 的答案,我会很高兴接受它。)

视口:

<Viewport3D x:Name="viewport">
    <Viewport3D.Resources>
        <Model3DGroup x:Key="luz_sombreada">
            <DirectionalLight  Color="White" Direction="-1,-1, -10" />
        </Model3DGroup>
        <Model3DGroup x:Key="luz_cheia">
            <AmbientLight  Color="White" />
        </Model3DGroup>
    </Viewport3D.Resources>
    <ModelVisual3D x:Name="luzes" Content="{StaticResource luz_cheia}" />

复选框:

<CheckBox Content="Sombreado" Checked="sombreado_Changed" Unchecked="sombreado_Changed" />

代码隐藏中的事件处理程序:

    private void sombreado_Changed(object sender, System.Windows.RoutedEventArgs e) {
        var nova_luz = new Model3DGroup();
        if (e.RoutedEvent.Name == "Checked") {
            nova_luz = (Model3DGroup)viewport.Resources["luz_sombreada"];
        }
        else if (e.RoutedEvent.Name == "Unchecked") {
            nova_luz = (Model3DGroup)viewport.Resources["luz_cheia"];
        }
        luzes.Content = nova_luz;
    }
于 2013-06-04T15:43:11.487 回答