WPF中的条件模板 [英] Conditional template in WPF

查看:317
本文介绍了WPF中的条件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个C#WPF应用程序.它有一个如下图所示的窗口:

Hi I have a C# WPF application. It has a window as image below:
The Image[^]

The list view has 3 columns. the first and the second are implemented:

<Window x:Class="InfoManager.Presentation_Layer.WindowProperty"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WindowProperty" Height="278" Width="480">
    <Grid>
        <ListView ItemsSource="{Binding PropertyCollection}"  Margin="12,86,12,12" Name="listView1">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="140"/>
                    <GridViewColumn Header="Type" Width="140">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Path=PropertyCollection}" Text="{Binding Path=PropertyTypeText}"  DisplayMemberPath="Value" SelectedValuePath="Key" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}" Width="140"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Button" Height="23" Margin="0,26,37,0" Name="buttonAdd" VerticalAlignment="Top" HorizontalAlignment="Right" Width="53" Click="buttonAdd_Click" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,30,0,0" Name="textBlock1" Text="Add new property:" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="116,27,96,0" Name="textBoxNewProperty" VerticalAlignment="Top" />
    </Grid>
</Window>




但是问题在于,第三列取决于第二列的值,在XAML中我们无法使用if命令.




But the problem is that the 3rd column depends on the value of the 2nd column and in XAML we cannot have if command.
what should I do?

推荐答案

您需要将组合框的SelectedItem绑定到codebehind/viewmodel中的属性,该属性返回与以下类型相同的对象作为组合框来源的集合.然后将第三列绑定到该属性.

因此,假设您的组合框列绑定到一组客户,那么您将在viewmodel/codebehind中的另一个属性中称为SelectedCustomer.您将ComboBox列的SelectedItem绑定到SelectedCustomer属性,将IsSynchronizedWithCurrentItem设置为true,还将第三列绑定到该SelectedCustomer属性.因此,当更改组合选择时,它将触发SelectedCustomer进行更改,这将更新第三列.

希望对您有帮助
You would need to bind the combobox''s SelectedItem to a property in the codebehind/viewmodel that returns an object of the same type as the collection that is the source of the combobox. Then you bind the third column to that property.

So say your combobox column is bound to a collection of customers, then you would have another property in the viewmodel/codebehind called SelectedCustomer. You would bind the ComboBox columns SelectedItem to the SelectedCustomer property, setting IsSynchronisedWithCurrentItem to true, and also bind the third column to that SelectedCustomer property. Thus when the combo selection is changed, it will trigger the SelectedCustomer to change which would update the third column.

Hope this helps


这篇关于WPF中的条件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆