现在我搜索了2天为什么我的Binding不起作用。我在PopUp中有一个ListView。我想将SelectedItem绑定到Code-Behind。我之前做过1298736次,但在这种情况下它不起作用。
这是在点击时打开弹出窗口的按钮:
<Button ToolTip="Emoticon einfügen" Name="SmileImg" Click="SmileImg_MouseLeftButtonDown">
<Image Source="..\Smileys\Smile.png" Stretch="None" SnapsToDevicePixels="True" ></Image>
</Button>
这是Popup定义
<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>
所以这是代码:
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 Binding效果很好。 Online SelectedItem不起作用。 SelectionChanged事件也没有解雇。
PS: 这只是一个原型,所以请专注于问题而不是编程风格:P
感谢您的帮助
UPDATE / Solution / Fixed ...无论如何:P
感谢您的所有答案。我找到了选择根本没有变化的原因......
private void RichtextBoxExt_GotFocus(object sender, RoutedEventArgs e)
{
//TextBox.Focus();
Debug.WriteLine("RichtextBoxExt_GotFocus");
}
CodeBehind中的这个事件处理程序每次点击一个项目时都会改变焦点,因此SelectedItem永远不会改变。
感谢您的想法!
答案 0 :(得分:1)
模式= OneWay OneWay updates the target property only when the source property changes.
<ListView IsSynchronizedWithCurrentItem="true" BorderThickness="0" Name="EmoList" SelectedItem="{Binding SelectedSmile, Mode=OneWay, ElementName=FsRichTextBoxControl}" SelectionMode="Single" ItemsSource="{Binding Emoticons, ElementName=FsRichTextBoxControl}" IsEnabled="False">
使用Mode = TwoWay。
答案 1 :(得分:1)
如果您的SelectedSmile绑定为Mode=OneWay
,您的视图(模型)将不会在更改时更新。
答案 2 :(得分:0)
<ListView IsSynchronizedWithCurrentItem="true" BorderThickness="0" Name="EmoList" SelectedItem="{Binding DataContext.SelectedSmile, Mode=TwoWay, ElementName=FsRichTextBoxControl}" SelectionMode="Single" ItemsSource="{Binding Emoticons, ElementName=FsRichTextBoxControl}" IsEnabled="False">
我认为这就是错误所在:
SelectedItem="{Binding DataContext.SelectedSmile, Mode=TwoWay, ElementName=FsRichTextBoxControl}"
答案 3 :(得分:0)
UPDATE / Solution / Fixed ...无论如何:P
感谢您的所有答案。我找到了选择根本没有变化的原因......
private void RichtextBoxExt_GotFocus(object sender, RoutedEventArgs e)
{
//TextBox.Focus();
Debug.WriteLine("RichtextBoxExt_GotFocus");
}
CodeBehind中的这个事件处理程序每次点击一个项目时都会改变焦点,因此SelectedItem永远不会改变。
感谢您的想法!