如何禁用ListView中的项目取消选择?

时间:2017-01-07 21:49:59

标签: wpf xaml

我有

<ListView SelectionMode="Single" SelectedIndex="0" ItemsSource="{Binding AccountViewModels}" SelectedItem="{Binding SelectedItem}" Style="{StaticResource AccountsList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <LocalViews:AccountView Margin="{StaticResource ControlMargin}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListView>

有没有办法禁止从ListView中取消选择项目(即ctrl+click)?换句话说,我不希望用户能够取消选择某个项目,但当然可以选择其他项目。

2 个答案:

答案 0 :(得分:2)

由于此功能纯粹与视图/控件相关,因此不应在视图模型中实现,但您可以像这样处理ListBoxItem容器的PreviewMouseLeftButtonDown事件:

$(document).ready(function () {
    var i=0;
    $("#add_row").click(function () {
        i=i+1;
        $('#tab_logic tbody').append("<tr><td>" + i + "</td><td><input name='name" + i + "' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail" + i + "' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='mobile" + i + "' type='text' placeholder='Mobile'  class='form-control input-md'></td><td><button type="button" class="delete">Delete</button></td></tr>");

        Test();
    });
    $(".delete").click(function () {
        $(this).parent().parent().remove();
    });
 });

 <div class="row clearfix">
    <div class="col-xs-4 column">
        <table class="table table-bordered table-hover" id="tab_logic">
            <thead>
                <tr >
                    <th class="text-center">
                        #
                    </th>
                    <th class="text-center">
                        Start Range
                    </th>
                    <th class="text-center">
                        End Range
                    </th>
                    <th class="text-center">
                        Value
                    </th>
                    <th class="text-center">
                        Action
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                    1
                    </td>
                    <td>
                    <input type="text" name='name0'  placeholder='Start Range' class="form-control"/>
                    </td>
                    <td>
                        <input type="text" name='mail0' placeholder='End Range' class="form-control"/>
                    </td>
                    <td>
                        <input type="text" name='mobile0' placeholder='Value' class="form-control"/>
                    </td>
                    <td>
                        <button type="button" class="delete">Delete</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<ListView SelectionMode="Single" SelectedIndex="0" ItemsSource="{Binding AccountViewModels}" SelectedItem="{Binding SelectedItem}" Style="{StaticResource AccountsList}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ItemPreviewMouseLeftButtonDown" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <LocalViews:AccountView Margin="{StaticResource ControlMargin}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListView>

答案 1 :(得分:0)

处理此问题的一种方法是使用绑定并添加逻辑以禁止在视图模型中取消选择。

this answer,更改IsSelected,如下所示:

sdk.dir = /home/kbuhiire/Android/Sdk