获取Selected ListBox项MVVM

时间:2018-05-03 18:29:57

标签: c# wpf

我只是做了以下事情:

public string  X
{
    get { return _X; }
    set
    {
        _X= value;
        NotifyOfPropertyChange("X");
    }
}

XAML:

<ListBox SelectedItem="{Binding X}" Grid.Row="0" Grid.Column="2" SelectedIndex="0" Margin="0 10 0 0">
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource MaterialDesignToolToggleListBoxItem}">
            <Setter Property="Padding" Value="8 12 8 12" />
        </Style>
    </ListBox.Resources>

    <ListBoxItem>
        <TextBlock Text="TEXT1"/>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock Text="TEXT2"/>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock Text="TEXT3"/>
    </ListBoxItem>
</ListBox>

我没有获得该项目,而是获得以下内容:

  

System.Windows.Controls.ListBoxItem:SELECTED_ITEM_HERE

3 个答案:

答案 0 :(得分:0)

您必须投射文本块并设置var json = new XMLHttpRequest(); json.open('GET', "http://localhost:8081/getPersons?format=json", true); json.send(); // It's better to declare event handlers this way, // your version relied on variable hoisting (look it up), // which is not usually recommended. json.onreadystatechange = function(e) { if (json.readyState == 4 && json.status == 200) { var response = JSON.parse(json.responseText); // This was the main problem: // Processing the result of the XHR should be *inside* the // readystate (or load) event handler for (var i = 0; i < response.length; i++) { // You forgot to put the property name "gender" in quotes // as well as the value "male" // Also, the double check was unnecessary. This is perfectly safe. if(response[i]['gender'] === 'male') { var img = document.createElement("img"); img.src = "http://advertdemo.ga/adverts/emotion_neutral/male/young/iphone.jpg"; // You should set dimensions on the style property, // not img directly img.style.width = "300px"; img.style.height = "300px"; document.body.appendChild(img); break; } } } } 文本

_x

答案 1 :(得分:0)

直接在xaml中指定listview项,您可以将这些值绑定到itemssource。

我尝试过以下代码段

...

在xaml中,

  public Class1()
        {
            TextList = new ObservableCollection<string> { "TEXT1", "TEXT2" };
        }
        private string _X;
        public string X
        {
            get { return _X; }
            set
            {
                _X = value;
                if (PropertyChanged != null)
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("X"));
            }
        }
        public ObservableCollection<string> TextList { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

有了这个我只得到“TEXT2”/“TEXT1”而不是控制类型。

答案 2 :(得分:0)

您应该向其添加ngOnChanges,而不是将ListBoxItems添加到ListBox

strings

另一种选择是将源属性的类型更改为<ListBox SelectedItem="{Binding X}" Grid.Row="0" Grid.Column="2" SelectedIndex="0" Margin="0 10 0 0" xmlns:s="clr-namespace:System;assembly=mscorlib"> <ListBox.Resources> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource MaterialDesignToolToggleListBoxItem}"> <Setter Property="Padding" Value="8 12 8 12" /> </Style> </ListBox.Resources> <s:String>TEXT1</s:String> <s:String>TEXT2</s:String> <s:String>TEXT3</s:String> </ListBox> ,但这不是您想要的。您绑定到ListBoxItem的属性的类型和属性的源属性必须匹配。