0

こんにちは、ComboBox でのバインドに問題があります。ComboBox アイテムを ListView 列にバインドし、選択した列で定義された添付プロパティの値を選択値として返したいと思います。

以下の例では、選択した列の幅を表示する作業サンプルを確認できます。ComboBox のSelectedValuePathを(loc:SampleBehavior.SampleValue)に変更しようとすると、バインド エラーが発生します。

BindingExpression パス エラー: '(u:SearchableListView.SearchMemberPath)' プロパティが 'object' ''GridViewColumn' に見つかりません

<Window x:Class="Problem_Sample1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:loc="clr-namespace:Problem_Sample1"
  WindowStartupLocation="CenterScreen"
  タイトル="Window1"
  高さ="300" 幅="300">
  <ドックパネル>
    <ComboBox DockPanel.Dock="トップ"
         x:Name="コンボボックス"
         ItemsSource="{Binding Path=View.Columns, ElementName=listview}"
         DisplayMemberPath="ヘッダー"
         SelectedValuePath="幅">
    </コンボボックス>

    <StatusBar DockPanel.Dock="Bottom">
      <テキストブロック>
        <TextBlock Text="選択した列 (値): " />
        <TextBlock Text="{Binding Path=SelectedValue, ElementName=combobox}" />
      </テキストブロック>
    </ステータスバー>

    <ListView x:Name="リストビュー">
      <ListView.View>
        <グリッドビュー>
          <GridViewColumn Header="名前"
                  幅="101"
                  loc:SampleBehavior.SampleValue="201" />
          <GridViewColumn Header="姓"
                  幅="102"
                  loc:SampleBehavior.SampleValue="202" />
        </GridView>
      </ListView.View>
    </ListView>
  </ドックパネル>
</ウィンドウ>

 

SampleBehavior.cs

System.Windows を使用します。
System.Windows.Controls を使用します。

名前空間 Problem_Sample1
{
  パブリック静的クラス SampleBehavior
  {

    public static readonly DependencyProperty SampleValueProperty =
      DependencyProperty.RegisterAttached(
        "サンプル値",
        typeof (int)、
        typeof (SampleBehavior));

    [AttachedPropertyBrowsableForType(typeof(GridViewColumn))]
    public static int GetSampleValue(GridViewColumn 列)
    {
      return (int)column.GetValue(SampleValueProperty);
    }

    [AttachedPropertyBrowsableForType(typeof(GridViewColumn))]
    public static void SetSampleValue(GridViewColumn 列、int 値)
    {
      column.SetValue(SampleValueProperty, 値);
    }

  }
}

 

助けや提案をありがとう。

4

1 に答える 1

1

私はこれに出くわしたので(そして、これはいくつかの合理的な検索に対する最初のグーグルの結果です)、今答えを書いた方がいいかもしれません.

要求された機能は、要求されたとおりに実際に利用できます。

<ComboBox DockPanel.Dock="Top"
     x:Name="combobox"
     ItemsSource="{Binding Path=View.Columns, ElementName=listview}"
     DisplayMemberPath="Header"
     SelectedValuePath="(loc:SampleBehavior.SampleValue)">

添付されたプロパティ パスを (中かっこ) で囲むことが重要です。そうしないと、ソース オブジェクトで奇妙なルックアップが試行されます。

また、質問のエラー メッセージには「BindingExpression path error: '(u:SearchableListView.SearchMemberPath)' property not found on 'object' ''GridViewColumn'」と記載されているため、エラー メッセージは完全に異なるプロパティに関連していることは間違いありません。 " (loc:SampleBehavior.SampleValue) " に。この矛盾は、コードサンプルを削減するために行われた編集に関連する問題のようです

于 2016-12-22T14:36:05.270 に答える