WPF ListView控件的SelectedItem绑定问题 [英] WPF ListView SelectedItem Binding Issue

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

问题描述

现在我搜索了2天,为什么我绑定不起作用。我有一个弹出窗口中一个ListView。我想则selectedItem绑定到code-后面。我过去就这么做过1298736次,但在这种情况下,这是行不通的。

now i searched for 2 Days why my Binding does not work. I have a ListView inside a PopUp. I Want to bind the SelectedItem to the Code-Behind. I've done that 1298736 times before, but in this case it does not work.

下面是打开弹出式窗口上点击按钮:

Here is the Button that opens the Popup on Click:

<Button  ToolTip="Emoticon einfügen" Name="SmileImg" Click="SmileImg_MouseLeftButtonDown">
    <Image   Source="..\Smileys\Smile.png" Stretch="None" SnapsToDevicePixels="True" ></Image>
</Button>

下面是弹出定义

Here is the Popup definition

<Popup AllowsTransparency="True" Name="SmiliesPopup" PopupAnimation="Fade" Placement="Top" PlacementTarget="{Binding ElementName=SmileImg}" StaysOpen="False" >
    <ListView IsSynchronizedWithCurrentItem="true" BorderThickness="0" Name="EmoList" SelectedItem="{Binding SelectedSmile, Mode=TwoWay, ElementName=FsRichTextBoxControl}" SelectionMode="Single" ItemsSource="{Binding Emoticons, ElementName=FsRichTextBoxControl}">

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel MaxWidth="50" Orientation="Horizontal"></WrapPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Image Width="16" Margin="2" Source="{Binding Uri}"></Image>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Popup>                 

因此​​,这里是code:

So here is the Code:

public Emoticon SelectedSmile
{
  get { return m_selectedSmile; }
  set
  {
    if (m_selectedSmile == null)
    {
      m_selectedSmile = value;
    }
    else
    {
      if (value != null && EmoList.SelectedItem != null)
      {
        var emo = (Emoticon)EmoList.SelectedItem;
        InsertImage(new BitmapImage(emo.Uri));
        SmiliesPopup.IsOpen = false;
        EmoList.SelectedItem = null;
      }
      m_selectedSmile = value;
    }

    InvokePropertyChanged(new PropertyChangedEventArgs("SelectedSmile"));

  }
}

...

public RichtextBoxExt()
{
  Emoticons = new AsyncObservableCollection<Emoticon>
                {
                  new Emoticon("Smile", new Uri("pack://application:,,,/TeamNote;component/Smileys/Smile.png")),
                  new Emoticon("Cheeky", new Uri("pack://application:,,,/TeamNote;component/Smileys/Cheeky.png")),
                  new Emoticon("Cry", new Uri("pack://application:,,,/TeamNote;component/Smileys/Cry.png")),
                  new Emoticon("Kiss", new Uri("pack://application:,,,/TeamNote;component/Smileys/Kiss.png")),
                  new Emoticon("Wink", new Uri("pack://application:,,,/TeamNote;component/Smileys/Wink.png")),
                  new Emoticon("Sad", new Uri("pack://application:,,,/TeamNote;component/Smileys/Sad.png")),
                  new Emoticon("Laugh", new Uri("pack://application:,,,/TeamNote;component/Smileys/Laugh.png"))
                };
  SelectedSmile = new Emoticon("Smile", new Uri("pack://application:,,,/TeamNote;component/Smileys/Smile.png"));
  InitializeComponent();
  Initialize();

...等

在的ItemsSource绑定效果很好。网上的SelectedItem不起作用。 SelectionChanged事件也没有开火。

The ItemsSource Binding works well. Online SelectedItem does not work. SelectionChanged Event is also not firing.

PS:
这仅仅是一个雏形,所以请专注于问题,而不是编程的风格:P

PS: This is just a prototype so please concentrate on the Problem and not on the style of programming :P

感谢您的帮助。

UPDATE /解决方案/固定...什么:P

感谢您的答案。我发现为什么选择不改变所有的原因...

Thanks for all your answers. I found the reason why the selection does not Change at all ...

private void RichtextBoxExt_GotFocus(object sender, RoutedEventArgs e)
{
  //TextBox.Focus();
  Debug.WriteLine("RichtextBoxExt_GotFocus");
}

这个事件处理程序中的codeBehind将焦点每次我点击一个项目,所以的SelectedItem永远不会改变。

This eventhandler in the CodeBehind changes the focus everytime i clicked on an item and so the SelectedItem never changes.

感谢您的想法!

推荐答案

UPDATE /解决方案/固定...什么:P

感谢您的答案。我发现为什么选择不改变所有的原因...

Thanks for all your answers. I found the reason why the selection does not Change at all ...

private void RichtextBoxExt_GotFocus(object sender, RoutedEventArgs e)
{
  //TextBox.Focus();
  Debug.WriteLine("RichtextBoxExt_GotFocus");
}

这个事件处理程序中的codeBehind将焦点每次我点击一个项目,所以的SelectedItem永远不会改变。

This eventhandler in the CodeBehind changes the focus everytime i clicked on an item and so the SelectedItem never changes.

感谢您的想法!

这篇关于WPF ListView控件的SelectedItem绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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