WPF ListView SelectedItem 绑定问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25179315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ListView SelectedItem Binding Issue
提问by S.L.
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.
现在我搜索了 2 天为什么我的绑定不起作用。我在 PopUp 中有一个 ListView。我想将 SelectedItem 绑定到代码隐藏。我之前已经完成了 1298736 次,但在这种情况下它不起作用。
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>
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();
... and so on.
... 等等。
The ItemsSource Binding works well. Online SelectedItem does not work. SelectionChanged Event is also not firing.
ItemsSource 绑定运行良好。在线 SelectedItem 不起作用。SelectionChanged 事件也没有触发。
PS: This is just a prototype so please concentrate on the Problem and not on the style of programming :P
PS:这只是一个原型,所以请专注于问题而不是编程风格:P
Thanks for your help
谢谢你的帮助
UPDATE / Solution / Fixed ... whatever :P
更新/解决方案/固定......无论如何: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");
}
This eventhandler in the CodeBehind changes the focus everytime i clicked on an item and so the SelectedItem never changes.
CodeBehind 中的这个事件处理程序在我每次点击一个项目时都会改变焦点,因此 SelectedItem 永远不会改变。
Thanks for your thoughts !
谢谢你的想法!
回答by Selva
Mode = OneWay OneWay updates the target property only when the source property changes.
Mode = OneWay OneWay 仅在源属性更改时更新目标属性。
<ListView IsSynchronizedWithCurrentItem="true" BorderThickness="0" Name="EmoList" SelectedItem="{Binding SelectedSmile, Mode=OneWay, ElementName=FsRichTextBoxControl}" SelectionMode="Single" ItemsSource="{Binding Emoticons, ElementName=FsRichTextBoxControl}" IsEnabled="False">
Use Mode = TwoWay.
使用模式 = TwoWay。
回答by Johannes Wanzek
If your SelectedSmile Binding is Mode=OneWayyour View(Model) will not getupdated on change.
如果您的 SelectedSmile 绑定是Mode=OneWay您的 View(Model) 将不会在更改时更新。
回答by user1
<ListView IsSynchronizedWithCurrentItem="true" BorderThickness="0" Name="EmoList" SelectedItem="{Binding DataContext.SelectedSmile, Mode=TwoWay, ElementName=FsRichTextBoxControl}" SelectionMode="Single" ItemsSource="{Binding Emoticons, ElementName=FsRichTextBoxControl}" IsEnabled="False">
I think this is where the error lay:
我认为这是错误所在:
SelectedItem="{Binding DataContext.SelectedSmile, Mode=TwoWay, ElementName=FsRichTextBoxControl}"
回答by S.L.
UPDATE / Solution / Fixed ... whatever :P
更新/解决方案/固定......无论如何: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");
}
This eventhandler in the CodeBehind changes the focus everytime i clicked on an item and so the SelectedItem never changes.
CodeBehind 中的这个事件处理程序在我每次点击一个项目时都会改变焦点,因此 SelectedItem 永远不会改变。
Thanks for your thoughts !
谢谢你的想法!

