从组合框启用列表框的字段 (C#-xaml)

Enable field of a Listbox from combobox (C#-xaml)

我想在选择组合框中的特定值时启用或禁用字段:

在我的 xaml 中,我有一个 ListBox (ListToTransfert) 是通过从另一个 Listbox (ListViewContents) 中拖动项目来填充的。

ListToTransfert 有 3 个字段:NameGapTime。 字段 Name 无法编辑,而其他 2 个字段可以编辑。

当组合框的值更改为 GapTime 时,我希望相应的列为 enabled,而另一列为 disabled

这是我的代码 XAML:

     <!--    ListViewContents  -->

        <Label Content="Contents" Background="White" HorizontalContentAlignment="Center" HorizontalAlignment="Left" Margin="11,16,0,0" VerticalAlignment="Top" Width="400" Height="31"/>
        <ListView Background="#bdc3c7" x:Name="ListViewContents" SelectionMode="Single" Margin="11,51.826,0,11" HorizontalAlignment="Left" Width="400">
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="Name" Width="350" DisplayMemberBinding="{Binding Name}"/>
                </GridView>
            </ListView.View>

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <command:EventToCommand Command="{Binding TransferContent}"  
                        CommandParameter="{Binding SelectedItem, ElementName=ListViewContents}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <Label Content="Label" Height="100" Width="100"/>
        </ListView>

     <!--    ListToTransfert  -->
     <TextBox Style="{StaticResource {x:Static ToolBar.TextBoxStyleKey}}" 
                     HorizontalContentAlignment="Center" 
                     HorizontalAlignment="Left" Margin="851,16,0,0" 
                     VerticalAlignment="Top" Width="400" Height="31" Text="{Binding groupName}" />

            <ListView Background="#bdc3c7" x:Name="ListToTransfert" ItemsSource="{Binding ContentsToTransfer}" HorizontalAlignment="Right" Width="400" Margin="0,51.826,21,11">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Amount" >
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition SharedSizeGroup="A" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Foreground="Black" Background="AliceBlue" Text="{Binding}"/>
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>

                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition SharedSizeGroup="A" />
                                        </Grid.ColumnDefinitions>

                                        <TextBlock Foreground="Black" Background="AliceBlue" HorizontalAlignment="Right" Text="{Binding Path=Name}"/>
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                <GridViewColumn Header="Gap"></GridViewColumn>
                <GridViewColumn Header="Time"></GridViewColumn>
                    </GridView>
                </ListView.View>

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <command:EventToCommand Command="{Binding DeleteContent}"  
                            CommandParameter="{Binding SelectedItem, ElementName=ListToTransfert}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </ListView>

我的组合框尚未创建,因为我不确定是否有解决此问题的方法,但总有一个。

你能给我一个 XAML 的解决方案吗?

这可以通过一组 RadioButton 或单个 ComboBox.

来完成

单选按钮

如果您不需要从视图模型控制此行为,您可以使用以下内容:

<RadioButton x:Name="TimeRadioButton" Content="Time" />

<GridViewColumn Header="Gap">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock IsEnabled="{Binding IsChecked, ElementName=GapRadioButton}"/>
            </Grid>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

或者,您可以在视图模型上添加 EnableGapColumn/EnableTimeColumn。但是,如果您创建一个枚举来定义此行为会更好:

public enum TransfertViewType
{
    Gap, Time
}

并更改与 EnumToBooleanConverter 的绑定:

<!-- put this in a resource -->
<converter:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />

<RadioButton Content="Time" IsChecked="{Binding ViewType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:YourViewModel.TransfertViewType}}"/>

<TextBlock IsEnabled="{Binding ViewType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:YourViewModel.TransfertViewType}}"/>

组合框

它与 RadioButton 部分的第二部分几乎相同,只是您需要创建一个 IEnumerable<TransfertViewType> 作为 ComboBoxItemsSource。或者,IDictionary<TransfertViewType, string> 如果您想稍微自定义一下描述。

<!-- for ienumerable -->
<ComboBox ItemsSource="{Binding TransfertViewTypes}"
          SelectedValue="{Binding ViewType}" />

<!-- for idictionary -->
<ComboBox ItemsSource="{Binding TransfertViewTypes}"
          DisplayMemberPath="Value" SelectedValuePath="Key"
          SelectedValue="{Binding ViewType}" />