【问题标题】:Binding Troubles ViewModel with Command使用命令绑定问题视图模型
【发布时间】:2016-03-18 18:46:01
【问题描述】:

我遇到了绑定问题。 可能我没看到。

XAML 文件

<ItemsControl Grid.Row="1" Grid.Column="1" x:Name="itemsControlTiles" ItemsSource="{Binding ProductCatalogLightList}" Margin="10">
            <ItemsControl.Template>
                <ControlTemplate>
                    <WrapPanel Width="800" HorizontalAlignment="Left"
                         FlowDirection="LeftToRight" IsItemsHost="true">
                    </WrapPanel>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Controls:Tile Title="{Binding Name}"
                           Margin="3"
                           Background="{Binding Background}"
                           Style="{StaticResource PrdukteTileStyle}" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding Source={StaticResource productsTileViewModel}, Path=DoSomethingCommand}" 
                                               CommandParameter="{Binding ID,ElementName= ProductCatalogLightList}" />

                            </i:EventTrigger>
                    </i:Interaction.Triggers>
                    </Controls:Tile>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

所以我的问题是 CommandParameter 绑定。

我的 ModelView 是这样的

public class Product_Catalog_Light
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int ID{ get; set; }
    public System.Windows.Media.Brush Background { get; set; }
}

public class ProductsTileViewModel : INotifyPropertyChanged
{
  private ObservableCollection<Product_Catalog_Light> _product_Catalog_LightList;
    public ProductsTileViewModel()
    {
        ProductCatalogLightList = new ObservableCollection<Product_Catalog_Light>();
    }
  ......
    public ObservableCollection<Product_Catalog_Light> ProductCatalogLightList
    {
        get { return _product_Catalog_LightList; }
        set
        {
            _product_Catalog_LightList = value;
            OnPropertyChanged("ProductCatalogLightList");
        }
    }

}

public ICommand DoSomethingCommand
    {
        get
        {
            return _doSomethingCommand ??
                   (_doSomethingCommand = new DelegateCommand<int>(ExecuteDoSomethingWithItem));
        }
    }

    private DelegateCommand<int> _doSomethingCommand;


    private void ExecuteDoSomethingWithItem(int db_ID )
    {
        // Do something wir the _id
        int i = 5;
    }

我收到一条错误消息,提示找不到绑定源。

System.Windows.Data 错误:4:找不到绑定源 参考“元素名称 = ProductCatalogLightList”。 绑定表达式:路径=ID;数据项=空;目标元素是 'InvokeCommandAction' (HashCode=11254959);目标属性是 'CommandParameter'(类型'对象')

【问题讨论】:

    标签: c# wpf xaml mvvm data-binding


    【解决方案1】:

    您没有名为 ProductCatalogLightList 的元素,而且您在触发器中的数据上下文已经是 Product_Catalog_Light,所以只需这样做:

    CommandParameter="{Binding ID}"
    

    ElementName 在绑定到 xaml 中控件的属性时使用,例如,如果您将 Control:Tile 命名为您可以绑定到其 Title 属性

    【讨论】:

    • 哦,是的...非常感谢!!
    • @Ebc:尝试使用Snoop。它使您可以遍历可视化树以查看/更改属性。它还可以帮助您设置控件的活动 DataContext。
    猜你喜欢
    • 2012-10-02
    • 2011-07-29
    • 1970-01-01
    • 2010-11-28
    • 2018-06-20
    • 2022-01-09
    • 2020-10-28
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多