将重点放在ListBox mvvm上

时间:2014-06-03 16:53:01

标签: wpf mvvm listbox focus

我有一个填充了值的ListBox,一个带有几个TextBox和的Grid 一个按钮。 在ListBox中选择元素时,相应的值将填充Grid中的TextBox。 Button用于清理TextBox。 所以,我的问题是:当我使用Button来清理TextBox时,该元素 在ListBox中仍然被选中。 我怎样才能取消选择'那个元素? 我是否需要重置ListBox的焦点? 如果是这种情况,是否可以在不在代码隐藏中编写代码的情况下执行此操作?

非常感谢。

以下是代码:

<ListBox Grid.Column="0"
    ItemsSource="{Binding AllItems, Mode=TwoWay}"
    DisplayMemberPath="intTrainningID"
    SelectedValuePath="intTrainningID">
    <i:Interaction.Triggers>
         <i:EventTrigger EventName="SelectionChanged">
              <cmd:EventToCommand Command="{Binding ListBoxCommand}"
                   MustToggleIsEnabledValue="True" 
                   PassEventArgsToCommand="True" />
         </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

<Grid Grid.Column="1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="25px"/>
        <RowDefinition Height="25px"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Label Content="TrainningID:" Grid.Column="0" Grid.Row="1" />
    <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding TrainningID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

    <Label Content="DateCreated:" Grid.Column="0" Grid.Row="2" />
    <DatePicker Grid.Column="1" Grid.Row="2" SelectedDate="{Binding DateCreated, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

    <ToolBarPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0">
    <Menu>
        <MenuItem Header="Clean" Command="{Binding CleanCommand}" CommandParameter="All Cleaned" />
    </Menu>
    </ToolBarPanel>

</Grid>

2 个答案:

答案 0 :(得分:0)

你能做的是:

将列表框SelectedValue绑定到MVVM中的属性,并在CleanCommand中将列表框selectedValue设置为null。

首先在mvvm中定义属性:

private YourType _selectedListBoxValue;
public YourType SelectedListBoxValue
{
    get { return _selectedListBoxValue; }
    set
    {
        if (_selectedListBoxValue!= value)
        {
            _selectedListBoxValue= value;
            RaisePropertyChanged("SelectedListBoxValue");
        }
    }
}

然后在列表框中绑定xaml:

SelectedValue="{Binding SelectedListBoxValue}"

最后使用CleanCommand将SelectedListBoxValue设置为null。

private void OnCleanCommand(){
SelectedListBoxValue = null;
}

答案 1 :(得分:0)

我重新发布整个代码以使其更清晰。

XAML:

<ListBox Grid.Column="0"
    ItemsSource="{Binding AllItems, Mode=TwoWay}"
    DisplayMemberPath="strDescription"
    SelectedValuePath="intTrainningID"
    SelectedValue="{Binding SelectedListBoxValue}">

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding ListBoxCommand}"
                MustToggleIsEnabledValue="True" 
                PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

<ToolBarPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0">
    <Menu>
        <MenuItem Header="Clean" Command="{Binding CleanCommand}" CommandParameter="All Cleaned" />
    </Menu>
</ToolBarPanel>

视图模型:

public MainWidowViewModel(MainWindow pView)
    {
        ..
        CleanCommand = new RelayCommand(new Action<object>(CleanAllItems));
        ..
    }

private List<T_Trainning> _allItems;
public List<T_Trainning> AllItems
{
    get
    {
        if (_allItems == null)
            _allItems = LoadItems();
        return _allItems;
    }
    private set
    {
        _allItems = value;
    }
}

private string _selectedListBoxValue;
public string SelectedListBoxValue
{
    get { return _selectedListBoxValue; }
    set
    {
        if (_selectedListBoxValue != value)
        {
            _selectedListBoxValue = value;
            OnPropertyChanged("SelectedListBoxValue");    
        }
    }
}

public void CleanAllItems(object obj)
{
    ..
    SelectedListBoxValue = null;
    ..
}

void LBItemSelected(object obj)
{
    SelectionChangedEventArgs e = (SelectionChangedEventArgs)obj;
    T_Trainning tra = (T_Trainning)e.AddedItems[0];
    TrainningID = tra.intTrainningID.Value;
    ..
}