WPF DataTrigger设置Width Window属性

时间:2013-11-29 12:24:49

标签: wpf xaml datatemplate

我想根据ViewModel属性的值设置Window的width属性。首先我有一个ContentControl:                   

然后:

    <DataTemplate DataType="{x:Type massedit:SimpleFieldVM}">
        <ContentControl Content="{Binding .}">
            <ContentControl.Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" />
                    <Style.Triggers>
                        <!-- Without selector, only with DataTriggers select my ContentControl -->
                        <DataTrigger Binding="{Binding TargetFieldType}" Value="{x:Static eva:FieldDataType.ENum}">
                            <Setter Property="ContentTemplate" Value="{StaticResource ComboboxEnumTemplate}" />
                            <Setter Property="Width" Value="100"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding TargetFieldType}" Value="{x:Static eva:FieldDataType.List}">
                            <Setter Property="ContentTemplate" Value="{StaticResource ComboboxListTemplate}" />
                            <Setter Property="Width" Value="100"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding TargetFieldType}" Value="{x:Static eva:FieldDataType.Bit}">
                            <Setter Property="ContentTemplate" Value="{StaticResource BitTemplate}" />
                            <Setter Property="Width" Value="100"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding TargetFieldType}" Value="{x:Static eva:FieldDataType.Date}">
                            <Setter Property="ContentTemplate" Value="{StaticResource DateTemplate}" />
                            <Setter Property="Width" Value="150"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding TargetFieldType}" Value="{x:Static eva:FieldDataType.DatTime}">
                            <Setter Property="ContentTemplate" Value="{StaticResource DatTimeTemplate}" />
                            <Setter Property="Width" Value="150"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding TargetFieldType}" Value="{x:Static eva:FieldDataType.Time}">
                            <Setter Property="ContentTemplate" Value="{StaticResource TimeTemplate}" />
                            <Setter Property="Width" Value="50"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </DataTemplate>

但是更改ContentControl的属性Width。我想更改我的窗口的属性Width!

1 个答案:

答案 0 :(得分:2)

您可以直接绑定Window.Width,也可以使用FitToContent属性。

http://msdn.microsoft.com/en-us/library/vstudio/ms743733%28v=vs.100%29.aspx

如果您设法将内容设置为窗口将遵循的所需大小,FitToContent将尝试使Window适合其内容。 :)