2012-01-02 12 views
1

私はListViewを持つb_StringBoolTemplateというUserControlを持っています。 ListViewのアイテムは、ItemsというDependencyPropertyにバインドされています。 ListViewには、テキストボックス用とチェックボックス用の2つの列があります。ユーザーコントロールのためのXAMLコードは:コードビハインドで ユーザーコントロールのListView列の表示メンバーをカスタムオブジェクトにバインドするにはどうすればよいですか?

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="AutoCleaner.b_StringBoolTemplate" 
    x:Name="b_StringBoolTemplate1" 
    d:DesignWidth="310" d:DesignHeight="374"> 

    <Grid> 
     <ListView ItemsSource="{Binding ElementName=b_StringBoolTemplate1, Path= Items}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="{Binding ElementName=b_StringBoolTemplate1, Path= StringLabel}" Width="150"> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <TextBox Text="{Binding ElementName=StringBoolInfo, Path=Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"/> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 

        <GridViewColumn Header="{Binding ElementName=b_StringBoolTemplate1, Path=BoolLabel}" Width="50" > 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <CheckBox IsChecked="{Binding BoolValue, ElementName=b_StringBoolInfo}"/> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 

        <GridViewColumn Width="50" > 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <Button Name="btnDeleteItem" Content="Delete"/> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 

       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
</UserControl> 

私のListViewItemから派生しStringContentとBOOLVALUEと呼ばれる2つの依存性を、持つb_StringBoolInfoと呼ばれるオブジェクトを持っています。 TextContext.TextをStringContentにバインドし、Checkbox.IsCheckedをBoolValueにバインドして、UserControlにb_StringBoolInfo型の項目を動的に追加することができます。

私の方法が最善の方法であるかどうかもわかりません。それが重要な場合に備えてヘッダーとItemsSourceをDendency Propsにバインドするとうまく動作します。

申し訳ありません私は少し曖昧です。事前にお返事いただきありがとうございます。

編集:申し訳ありませんが、私はそれを明確にしませんでした。 ウィンドウにあるUserControlのインスタンスにb_StringBoolInfo型の項目を追加しました。 TextBox.TextとCheckbox.IsCheckedをb_StringBoolのプロパティにバインドしようとしていますが、その方法はわかりません。 ListViewItem由来私はb_StringBoolInfoというクラスを書いたコードで

:私はb_StringBoolTemplateと呼ばれるウィンドウにユーザーコントロール、ユーザーコントロールオブジェクト以下のXAMLを追加した

public class b_StringBoolInfo :ListViewItem 
    { 


     public string StringContent 
     { 
      get { return (string)GetValue(StringContentProperty); } 
      set { SetValue(StringContentProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for StringContent. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty StringContentProperty = 
      DependencyProperty.Register("StringContent", typeof(string), typeof(b_StringBoolInfo),new PropertyMetadata("String")); 



     public bool BoolValue 
     { 
      get { return (bool)GetValue(BoolValueProperty); } 
      set { SetValue(BoolValueProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for BoolValue. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty BoolValueProperty = 
      DependencyProperty.Register("BoolValue", typeof(bool), typeof(b_StringBoolInfo), new PropertyMetadata(true)); 



    } 

、及びそのアイテム

<local:b_StringBoolTemplate Margin="64,67,159,145" StringLabel="Folder Location" BoolLabel="Search SubFolders?"> 
    <local:b_StringBoolTemplate.Items> 

     <local:b_StringBoolInfo StringContent="String" BoolValue="True"/> 

    </local:b_StringBoolTemplate.Items> 
</local:b_StringBoolTemplate> 
+1

は**ない**は何の作業を語りました。 –

+0

申し訳ありません私はOPを編集したので、明確にしませんでした。テキストボックスのテキストをStringContentにバインドしようとしています。チェックボックスをBoolValueに設定していますが、どちらもb_StringBoolInfoというカスタムオブジェクトのプロパティです。 –

+0

しかし、私がバインドしようとするものは何も動作しません。私も 'Text = {Binding StringContent}'を試しましたが、うまくいきません。 –

答えて

0

問題がされホワイティング次の行:

<TextBox Text="{Binding ElementName=StringBoolInfo, Path=Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" /> 

<CheckBox IsChecked="{Binding BoolValue, ElementName=b_StringBoolInfo}" /> 

あなたはb_StringBoolInfoを述べたが背後にあるコード内の変数です。しかし、

ElementName = b_StringBoolInfo 

では、名前b_StringBoolInfoであなたのXAMLファイル内要素/コントロールを探しにXAMLパーサを教えてください。

変更をそれを修正するには、以下にそれらのライン:あなたはまだhaventは

<TextBox Text="{Binding Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" /> 

<CheckBox IsChecked="{Binding BoolValue}" /> 
+0

混乱して申し訳ありませんが、b_StringBoolInfoは変数ではなくカスタムオブジェクトです。元の投稿にクラスのコードを追加しました。 –

+0

私はUserControlにタイプb_StringBool情報の項目を追加したいと思いますが、TextBox.TextとCheckbox.IsCheckedをb_StringBoolのプロパティにバインドしようとしていますが、その方法はわかりません。 UserControlを持つウィンドウのxamlコードの一部を追加しました。 –

関連する問題